<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是天猫蓝牙Mesh项目——进阶开发 实现配网器代码 的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV04-天猫蓝牙Mesh项目-02-进阶开发-09-实现配网器代码">
<meta property="og:url" content="https://sumumm.github.io/post/a6277951.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是天猫蓝牙Mesh项目——进阶开发 实现配网器代码 的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240106074405981.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240106121737109.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240106121134403.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240107095929928.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240107111752251.png">
<meta property="article:published_time" content="2024-01-07T03:46:12.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.030Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV21-天猫精灵IOT">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240106074405981.png">


<link rel="canonical" href="https://sumumm.github.io/post/a6277951.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/a6277951.html","path":"post/a6277951.html","title":"LV04-天猫蓝牙Mesh项目-02-进阶开发-09-实现配网器代码"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV04-天猫蓝牙Mesh项目-02-进阶开发-09-实现配网器代码 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E7%BD%91%E7%BB%9C%E5%B1%82%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95"><span class="nav-text">一、网络层数据结构与算法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%85%88%E7%9C%8B%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0"><span class="nav-text">1. 先看一个函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-net-buf-simple"><span class="nav-text">2. 数据结构 net_buf_simple</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%AE%9A%E4%B9%89%E6%98%AF%E6%80%8E%E6%A0%B7%E7%9A%84%EF%BC%9F"><span class="nav-text">2.1 定义是怎样的？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-net-buf-simple%E5%BA%94%E7%94%A8%E8%A7%A3%E6%9E%90"><span class="nav-text">2.2 net_buf_simple应用解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-buf-h-%E5%88%86%E6%9E%90"><span class="nav-text">3. buf.h 分析</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E9%85%8D%E7%BD%91%E5%99%A8%E5%8A%9F%E8%83%BD%E5%88%86%E6%9E%90"><span class="nav-text">二、配网器功能分析</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%85%8D%E7%BD%91%E5%99%A8%E5%AE%9E%E7%8E%B0"><span class="nav-text">三、配网器实现</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%B7%A5%E7%A8%8B%E4%BF%AE%E6%94%B9"><span class="nav-text">1. 工程修改</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-helloworld-mk"><span class="nav-text">1.1 helloworld.mk</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-bt-mesh-mk"><span class="nav-text">1.2 bt_mesh.mk</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BF%AE%E6%94%B9%E9%83%A8%E5%88%86%E6%96%87%E4%BB%B6"><span class="nav-text">2. 初始化修改部分文件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-net-h"><span class="nav-text">2.1 net.h</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-main-h"><span class="nav-text">2.2 main.h</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-crypto-c"><span class="nav-text">2.3 crypto.c</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-adv-c"><span class="nav-text">2.4 adv.c</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-beacon-c"><span class="nav-text">2.5 beacon.c</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-main-c"><span class="nav-text">2.6 main.c</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7-genie-mesh-c"><span class="nav-text">2.7 genie_mesh.c</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%85%8D%E7%BD%91%E5%99%A8%E9%85%8D%E7%BD%91%E8%BF%87%E7%A8%8B%E9%83%A8%E5%88%86%E5%AE%9E%E7%8E%B0"><span class="nav-text">3. 配网器配网过程部分实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-provisioner-prov-h"><span class="nav-text">3.1 provisioner_prov.h</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-provisioner-prov-c"><span class="nav-text">3.2 provisioner_prov.c</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%B5%8B%E8%AF%95%E7%8E%B0%E8%B1%A1"><span class="nav-text">四、测试现象</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/a6277951.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV04-天猫蓝牙Mesh项目-02-进阶开发-09-实现配网器代码 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV04-天猫蓝牙Mesh项目-02-进阶开发-09-实现配网器代码
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-01-07 11:46:12" itemprop="dateCreated datePublished" datetime="2024-01-07T11:46:12+08:00">2024-01-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/" itemprop="url" rel="index"><span itemprop="name">LV21-天猫精灵IOT</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>13k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>47 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是天猫蓝牙Mesh项目——进阶开发 实现配网器代码 的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows版本</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu版本</td>        <td align="left">Ubuntu22.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="6">官方网站</td>        <td align="left"><a href="https://cn.aliyun.com/" target="_blank">阿里云</a></td>        <td align="left">阿里云官网主页</td>    </tr>    <tr>        <td align="left"><a href="https://www.aliyun.com/product/iot/livinglink?spm=5176.28508143.J_4VYgf18xNlTAyFFbOuOQe.317.5421154acZniIL&scm=20140722.X_data-1da961ea118f5bb0e4b2._.V_1" target="_blank">阿里生活物联平台</a></td>        <td align="left">生活物联网平台(飞燕平台)主页</td>    </tr>    <tr>        <td align="left"><a href="https://iap.aligenie.com/?spm=0.7629140.0.0.2c611780Ty6tzg" target="_blank">AliGenie</a></td>        <td align="left">天猫精灵开放平台AliGenie主页</td>    </tr>    <tr>        <td align="left"><a href="https://www.aliyun.com/product/iot/iot_instc_public_cn?spm=5176.28508143.J_4VYgf18xNlTAyFFbOuOQe.81.5421154au3ocqp&scm=20140722.X_data-9349178ba84cf028806a._.V_1" target="_blank">阿里物联网平台</a></td>        <td align="left">阿里物联网平台主页</td>    </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/" target="_blank">Bluetooth 技术网站</a></td>        <td align="left">蓝牙协议规范什么的可以来这里找</td>    </tr>    <tr>        <td align="left"><a href="https://www.telink-semi.com/" target="_blank">Telink</a></td>        <td align="left">Telink | Chips for a Smarter IoT (telink-semi.com)<br><a href="https://www.telink-semi.cn/" target="_blank">Telink中文官网</a></td>    </tr>    <tr>        <td align="center" rowspan="5">开发手册</td>        <td align="left"><a href="https://help.aliyun.com/zh/alios-things/" target="_blank">AliOS Things开发指南</a></td>        <td align="left">AliOS Things开发指南，这里是最新版本，可以直接从官网找到</td>    </tr>     <tr>        <td align="left"><a href="https://haas.iot.aliyun.com/aliosthings/index.html" target="_blank">AliOS Things开发指南</a></td>        <td align="left">AliOS Things应用开发指南，这里应该是3.3版本的完整开发文档</td>    </tr>    <tr>        <td align="left"><a href="https://gaic.alicdn.com/aic/h5_daily/test/aliosthings/3.0.13/index.html" target="_blank">AliOS Things开发指南(3.0)</a></td>        <td align="left">AliOS Things应用开发指南，这里应该是3.0版本的完整开发文档</td>    </tr>    <tr>        <td align="left"><a href="https://help.aliyun.com/product/123207.html?spm=a2c4g.11186623.6.540.114e60d84sF58G" target="_blank">生活物联网平台开发文档</a></td>        <td align="left">生活物联网平台（飞燕平台）开发文档<br><a href="https://help.aliyun.com/document_detail/180778.html?spm=a2c4g.123207.0.0.49556adcO5apvd" target="_blank">《设备端开发指南》</a></td>    </tr>    <tr>        <td align="left"><a href="https://doc-bot.tmall.com/docs/doc.htm?spm=0.7629140.0.0.7bf81780UN7WkQ&treeId=578&articleId=118057&docType=1" target="_blank">Wi-Fi IoT品类定义与功能开发</a></td>        <td align="left">天猫精灵IoT开放平台——Wi-Fi IoT品类定义与功能开发</td>    </tr>    <tr>        <td align="center" rowspan="3">硬件平台</td>        <td align="left"><a href="https://developer.aliyun.com/article/761644?spm=a2c6h.12873581.0.dArticle761644.1c1b589dRHgvcR&groupCode=genieaiot" target="_blank">mk3080 WiFi开发板</a></td>        <td align="left">WiFi开发板使用指南-阿里云开发者社区</td>    </tr>    <tr>        <td align="left"><a href="http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/esp8266-nodemcu-tutorial-index/" target="_blank">esp8266开发板</a></td>        <td align="left">一个教程：ESP8266-NodeMCU开发板详解-太极创客 (taichi-maker.com)</td>    </tr>    <tr>        <td align="left"><a href="https://wiki.telink-semi.cn/doc/ds/DS_TLSR8258-E_Datasheet%20for%20Telink%20BLE+IEEE802.15.4%20Multi-Standard%20Wireless%20SoC%20TLSR8258.pdf" target="_blank">TLSR8258 Datasheet</a></td>        <td align="left">Datasheet for Telink BLE + IEEE802.15.4 MultiStandard Wireless SoC TLSR8258</td>    </tr>    <tr>        <td align="center" rowspan="2">参考资料</td>        <td align="left"><a href="https://developer.aliyun.com/article/720516#" target="_blank">AliOS Things 3.0 应用开发指南</a></td>        <td align="left">这个只是一篇参考文章，里面是一些环境搭建相关的，可以参考</td>    </tr>    <tr>        <td align="left"><a href="https://info.support.huawei.com/info-finder/encyclopedia/zh/index.html" target="_blank">IP知识百科 - 华为 (huawei.com)</a></td>        <td align="left">IP的一些相关知识点</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="left" rowspan="6">蓝牙规范相关文档</td>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/specs/core-specification-5-2/" target="_blank">Core Specification 5.2</a></td>        <td align="left">核心规格 5.2，该规范定义了创建可互操作的Bluetooth 设备所需的技术。                        <br><a href="./蓝牙参考资料/Core_v5.2.pdf" target="_blank">《Core_v5.2.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/specs/mesh-model-1-1/" target="_blank">Mesh Model(v1.1)</a></td>        <td align="left">本Bluetooth 规范定义了模型（以及它们所需的状态和消息），这些模型用于在mesh 网络中的节点上执行基本功能，超出了Bluetooth Mesh 配置文件 规范中定义的基础模型。                        <br>本规范包括定义跨设备类型标准功能的通用模型，以及支持关键mesh 场景的模型，如照明控制、传感器、时间和场景。                        <br><a href="./蓝牙参考资料/MshMDL_v1.1.pdf" target="_blank">《MshMDL_v1.1.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/specs/mesh-profile-1-0-1/" target="_blank">Mesh Profile(v1.0.1)</a></td>        <td align="left">该Bluetooth 规范定义了基本要求，以实现可互操作的mesh 网络解决方案，用于Bluetooth 低能量无线技术。                        <br><a href="./蓝牙参考资料/MshPRFv1.0.1.pdf" target="_blank">《MshPRFv1.0.1.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/zh-cn/specifications/device-properties/" target="_blank">Mesh Device Properties</a></td>        <td align="left">本规范包含Bluetooth Mesh 配置文件 和Bluetooth Mesh 模型规范所要求的设备属性的定义。                        <br>但是跟之前的有些区别，我主要看的之前的版本：<a href="./蓝牙参考资料/MeshDeviceProperties_v1.2.pdf" target="_blank">《MMeshDeviceProperties_v1.2.pdf》</a></td>      </tr>    <tr>        <td align="left"><a href="https://www.bluetooth.com/specifications/gss/" target="_blank">GATT Specification Supplement</a></td>        <td align="left">GATT Specification Supplement | Bluetooth® Technology Website。                        <br>好像可以在线看：<a href="https://btprodspecificationrefs.blob.core.windows.net/gatt-specification-supplement/GATT_Specification_Supplement.pdf" target="_blank">《GATT Specification Supplement》</a></td>      </tr>     <tr>        <td align="left"><a href="https://www.bluetooth.com/specifications/assigned-numbers/" target="_blank">Assigned Numbers</a></td>        <td align="left">GATT的一些类型定义可以在这里找。</td>      </tr>    <tr>        <td align="left" rowspan="2">AliOS Things</td>        <td align="left"><a href="https://gitee.com/alios-things-admin/AliOS-Things" target="_blank">alios-things/AliOS-Things</a></td>        <td align="left">Gitee上的AliOSThings SDK源码仓库</td>      </tr>    <tr>        <td align="left"><a href="https://github.com/alibaba/AliOS-Things" target="_blank">alibaba/AliOS-Things</a></td>        <td align="left">GitHub上的AliOSThings SDK源码仓库</td>      </tr>    <tr>        <td align="left" rowspan="2">天猫精灵蓝牙Mesh协议栈</td>        <td align="left"><a href="https://github.com/alibaba-archive/genie-bt-mesh-stack" target="_blank">alibaba-archive/genie-bt-mesh-stack</a></td>        <td align="left">GitHub上的天猫精灵蓝牙Mesh协议栈源码仓库。<br>之前是在<a href="https://github.com/alibaba/genie-bt-mesh-stack"target="_blank">alibaba/genie-bt-mesh-stack</a>这个仓库。<br>写笔记的时候最新提交为<a href="https://github.com/alibaba-archive/genie-bt-mesh-stack/tree/faf523618a6a2560090fc423222b9db80984bb7a" target="_blank">faf523618a6a2560090fc423222b9db80984bb7a</a></td>      </tr>    <tr>        <td align="left"><a href="https://help.aliyun.com/document_detail/171552.html?spm=a2c4g.173310.0.0.55d6e992e8xJip" target="_blank">蓝牙Mesh设备开发指南</a></td>        <td align="left">阿里云生活服务平台开发手册——蓝牙设备开发一节中的内容</td>      </tr></table>
              </div>
            </details>

<p>这一LV的笔记起始跟前面有所重叠，互相补充吧算是。前面我们知道天猫精灵智能音箱可以对泰凌微8258蓝牙模块进行配网，它使用的是静态OOB的方式，上一节，我们通过手机，使用nRF Mesh软件，让手机作为配网器，以无OOB的方式完成对模块的配网。这一节，我们在一个蓝牙模块上实现配网器功能，用它来给另一个蓝牙模块配网。为什么要实现配网器的代码？因为我们前面下载的阿里提供的协议栈实现的是未配网设备的功能，配网器的功能是天猫精灵智能音箱的。</p>
<h1 id="一、网络层数据结构与算法"><a href="#一、网络层数据结构与算法" class="headerlink" title="一、网络层数据结构与算法"></a><font size=3>一、网络层数据结构与算法</font></h1><p>需要知道的是，arm架构的处理器中的数据是以小端模式进行存储，而网络传输的数据是以大端模式存储的，需要注意。</p>
<blockquote>
<p>Tips：大端模式与小端模式：</p>
<p>小端模式：数据的<strong>低位</strong>放在<strong>低地址</strong>空间，数据的<strong>高位</strong>放在<strong>高地址</strong>空间。</p>
<p>大端模式：数据的<strong>高位</strong>放在<strong>低地址</strong>空间，数据的<strong>低位</strong>放在<strong>高地址空间</strong>。</p>
</blockquote>
<h2 id="1-先看一个函数"><a href="#1-先看一个函数" class="headerlink" title="1. 先看一个函数"></a><font size=3>1. 先看一个函数</font></h2><p>我们先看一个函数，就是邀请配网的这个函数：network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;src&#x2F;prov.c：prov_invite()</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240106074405981.png" alt="image-20240106074405981" />

<p>这里有一个我们需要了解的结构体，就是net_buf_simple。</p>
<h2 id="2-数据结构-net-buf-simple"><a href="#2-数据结构-net-buf-simple" class="headerlink" title="2. 数据结构 net_buf_simple"></a><font size=3>2. 数据结构 net_buf_simple</font></h2><h3 id="2-1-定义是怎样的？"><a href="#2-1-定义是怎样的？" class="headerlink" title="2.1 定义是怎样的？"></a><font size=3>2.1 定义是怎样的？</font></h3><p>这个结构体定义在network&#x2F;bluetooth&#x2F;bt_common&#x2F;include&#x2F;net&#x2F;buf.h中：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** @brief Simple network buffer representation.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  This is a simpler variant of the net_buf object (in fact net_buf uses</span></span><br><span class="line"><span class="comment"> *  net_buf_simple internally). It doesn&#x27;t provide any kind of reference</span></span><br><span class="line"><span class="comment"> *  counting, user data, dynamic allocation, or in general the ability to</span></span><br><span class="line"><span class="comment"> *  pass through kernel objects such as FIFOs.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  The main use of this is for scenarios where the meta-data of the normal</span></span><br><span class="line"><span class="comment"> *  net_buf isn&#x27;t needed and causes too much overhead. This could be e.g.</span></span><br><span class="line"><span class="comment"> *  when the buffer only needs to be allocated on the stack or when the</span></span><br><span class="line"><span class="comment"> *  access to and lifetime of the buffer is well controlled and constrained.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> &#123;</span></span><br><span class="line">    <span class="comment">/** Pointer to the start of data in the buffer. */</span></span><br><span class="line">    <span class="type">u8_t</span> *data;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** Length of the data behind the data pointer. */</span></span><br><span class="line">    <span class="type">u16_t</span> len;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** Amount of data that this buffer can store. */</span></span><br><span class="line">    <span class="type">u16_t</span> size;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** Start of the data storage. Not to be accessed directly</span></span><br><span class="line"><span class="comment">     *  (the data pointer should be used instead).</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="type">u8_t</span> __buf[<span class="number">0</span>] __net_buf_align;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>各个成员的含义如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240106121737109.png" alt="image-20240106121737109" />

<h3 id="2-2-net-buf-simple应用解析"><a href="#2-2-net-buf-simple应用解析" class="headerlink" title="2.2 net_buf_simple应用解析"></a><font size=3>2.2 net_buf_simple应用解析</font></h3><p>这里就很有意思了，触及我的知识盲区，居然有一个0长度的数组，感觉看的不是很明白，我们下面放在代码中理解一下，首先是这里，定义了一个指针buf：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_invite</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">12</span>);</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们看一下这个PROV_BUF是什么：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_GATT)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_BUF_HEADROOM 5</span></span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_BUF_HEADROOM 0</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">rx_buf</span> =</span> NET_BUF_SIMPLE(<span class="number">65</span>);</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_BUF(len) NET_BUF_SIMPLE(PROV_BUF_HEADROOM + len)</span></span><br></pre></td></tr></table></figure>

<p>这里我们按没有定义CONFIG_BT_MESH_PB_GATT来算，也就是PROV_BUF_HEADROOM&#x3D;0，这样我们将PROV_BUF展开可以看到：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_BUF(12) NET_BUF_SIMPLE(12)</span></span><br></pre></td></tr></table></figure>

<p>这个NET_BUF_SIMPLE是什么？我们继续往下追：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** @def NET_BUF_SIMPLE</span></span><br><span class="line"><span class="comment"> *  @brief Define a net_buf_simple stack variable and get a pointer to it.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  This is a helper macro which is used to define a net_buf_simple object on</span></span><br><span class="line"><span class="comment"> *  the stack and the get a pointer to it as follows:</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  struct net_buf_simple *my_buf = NET_BUF_SIMPLE(10);</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  After creating the object it needs to be initialized by calling</span></span><br><span class="line"><span class="comment"> *  net_buf_simple_init().</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  @param _size Maximum data storage for the buffer.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *  @return Pointer to stack-allocated net_buf_simple object.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NET_BUF_SIMPLE(_size)                        \</span></span><br><span class="line"><span class="meta">    ((struct net_buf_simple *)(&amp;(struct &#123;        \</span></span><br><span class="line"><span class="meta">        struct net_buf_simple buf;           \</span></span><br><span class="line"><span class="meta">        u8_t data[_size] __net_buf_align; \</span></span><br><span class="line"><span class="meta">    &#125;) &#123;                                         \</span></span><br><span class="line"><span class="meta">        .buf.size = _size,                   \</span></span><br><span class="line"><span class="meta">    &#125;))</span></span><br></pre></td></tr></table></figure>

<p>看起来好像还挺复杂的，我们先把参数传进去然后写成种基本可以辨别的样子：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> NET_BUF_SIMPLE(12)                        \</span></span><br><span class="line"><span class="meta">    (</span></span><br><span class="line">		(<span class="keyword">struct</span> net_buf_simple *)</span><br><span class="line">		(</span><br><span class="line">            &amp;(<span class="keyword">struct</span> &#123;</span><br><span class="line">                    <span class="keyword">struct</span> net_buf_simple buf; </span><br><span class="line">                    <span class="type">u8_t</span> data[<span class="number">12</span>] __net_buf_align;</span><br><span class="line">   				&#125;) </span><br><span class="line">        	&#123;</span><br><span class="line">            	.buf.size = <span class="number">12</span>, <span class="comment">// 这里就很神奇，在定义这个结构体成员的时候直接把 buf 成员里面的 size成员直接初始化了，我还是第一次见这种方式。</span></span><br><span class="line">        	&#125;</span><br><span class="line">        )</span><br><span class="line">	)</span><br></pre></td></tr></table></figure>

<p>惊呆了啊，这里居然直接把定义的结构体给初始化了，然后还把地址返回了，就像是定义了一个变量，然后将变量地址返回。后来查了一下，发现这里就是C语言中0长度数组的应用，也就是“可变数组”，我们总结一下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240106121134403.png" alt="image-20240106121134403" />

<p>其实struct定义结构体的时候可以直接定义变量，甚至可以没有变量名，我们要知道变量名的意义其实就是帮助我们找到数据在内存中的位置，我们完全可以直接用指针指向这片内存而不需要变量名的。可以写一个实例看一下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __net_buf_align __attribute__ ((__aligned__(sizeof(int))))</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> &#123;</span></span><br><span class="line">    <span class="comment">/** Pointer to the start of data in the buffer. */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> *data;</span><br><span class="line">    <span class="comment">/** Length of the data behind the data pointer. */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> len;</span><br><span class="line">    <span class="comment">/** Amount of data that this buffer can store. */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> size;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** Start of the data storage. Not to be accessed directly</span></span><br><span class="line"><span class="comment">     *  (the data pointer should be used instead).</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> __buf[<span class="number">0</span>];</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NET_BUF_SIMPLE(_size)                       \</span></span><br><span class="line"><span class="meta">    ((struct net_buf_simple *)(&amp;(struct &#123;           \</span></span><br><span class="line"><span class="meta">        struct net_buf_simple buf;                  \</span></span><br><span class="line"><span class="meta">        unsigned short data[_size] __net_buf_align; \</span></span><br><span class="line"><span class="meta">    &#125;) &#123;                                            \</span></span><br><span class="line"><span class="meta">        .buf.size = _size,                          \</span></span><br><span class="line"><span class="meta">    &#125;))</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">const</span> <span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;array test!\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> NET_BUF_SIMPLE(<span class="number">12</span>);</span><br><span class="line">	<span class="type">int</span> *p = <span class="literal">NULL</span>;</span><br><span class="line">	</span><br><span class="line">	buf-&gt;data = buf-&gt;__buf;</span><br><span class="line">	buf-&gt;len = <span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;buf = %p, sizeof(net_buf_simple)=%ld\n&quot;</span>, buf, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> net_buf_simple));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;buf-&gt;data = %p\n&quot;</span>, buf-&gt;data);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;buf-&gt;__buf = %p\n&quot;</span>, buf-&gt;__buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;buf-&gt;len = %d\n&quot;</span>, buf-&gt;len);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;buf-&gt;size = %d\n&quot;</span>, buf-&gt;size);</span><br><span class="line">	</span><br><span class="line">	p = (<span class="type">int</span> *)(&amp;(<span class="keyword">struct</span> &#123;</span><br><span class="line">		<span class="type">int</span> a;</span><br><span class="line">	&#125;)&#123;.a = <span class="number">10</span>,&#125;);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;a = %d\n&quot;</span>, *p);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="3-buf-h-分析"><a href="#3-buf-h-分析" class="headerlink" title="3. buf.h 分析"></a><font size=3>3. buf.h 分析</font></h2><p>文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;util&#x2F;buf.h</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//四字节对齐宏</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __net_buf_align __aligned(sizeof(int))</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> &#123;</span></span><br><span class="line">    </span><br><span class="line">    <span class="type">u8_t</span> *data;		<span class="comment">/** 指向缓冲区中数据开头的指针 */</span></span><br><span class="line">    <span class="type">u16_t</span> len;		<span class="comment">/** 数据指针后面的剩余数据长度 */</span></span><br><span class="line">    <span class="type">u16_t</span> size;		<span class="comment">/** 此缓冲区可以存储的数据量 */</span></span><br><span class="line">    <span class="type">u8_t</span> __buf[<span class="number">0</span>] __net_buf_align;	<span class="comment">/** 数据存储的起始地址。不直接访问（应使用数据指针）*/</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> NET_BUF_SIMPLE(_size)                        \</span></span><br><span class="line"><span class="meta">	((struct net_buf_simple *)(&amp;(struct &#123;        \</span></span><br><span class="line"><span class="meta">		struct net_buf_simple buf;           \		<span class="comment">//</span></span></span><br><span class="line">		<span class="type">u8_t</span> data[_size] __net_buf_align; \			<span class="comment">//实例化的数据存储数组</span></span><br><span class="line">	&#125;) &#123;                                         \</span><br><span class="line">		.buf.size = _size,                   \</span><br><span class="line">	&#125;))</span><br><span class="line"></span><br><span class="line"><span class="comment">//初始化缓存区</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> net_buf_simple_init(<span class="keyword">struct</span> net_buf_simple *buf,</span><br><span class="line">				       <span class="type">size_t</span> reserve_head)</span><br><span class="line">&#123;</span><br><span class="line">	buf-&gt;data = buf-&gt;__buf + reserve_head;</span><br><span class="line">	buf-&gt;len = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//计算头部空间有多少</span></span><br><span class="line"><span class="type">size_t</span> <span class="title function_">net_buf_simple_headroom</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> buf-&gt;data - buf-&gt;__buf;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//计算尾部空间有多少</span></span><br><span class="line"><span class="type">size_t</span> <span class="title function_">net_buf_simple_tailroom</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> buf-&gt;size - net_buf_simple_headroom(buf) - buf-&gt;len;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//尾部追加（只增加剩余长度，实际未添加数据）</span></span><br><span class="line"><span class="type">void</span> *<span class="title function_">net_buf_simple_add</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">size_t</span> len)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//找到尾部指针</span></span><br><span class="line">	<span class="type">u8_t</span> *tail = net_buf_simple_tail(buf);		</span><br><span class="line"></span><br><span class="line">	NET_BUF_SIMPLE_DBG(<span class="string">&quot;buf %p len %zu&quot;</span>, buf, len);</span><br><span class="line">	<span class="comment">//判断是否越界</span></span><br><span class="line">	NET_BUF_SIMPLE_ASSERT(net_buf_simple_tailroom(buf) &gt;= len);</span><br><span class="line">	<span class="comment">//增加剩余长度</span></span><br><span class="line">	buf-&gt;len += len;</span><br><span class="line">	<span class="keyword">return</span> tail;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//尾部添加小端32位数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">net_buf_simple_add_le32</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">u32_t</span> val)</span></span><br><span class="line">&#123;</span><br><span class="line">	NET_BUF_SIMPLE_DBG(<span class="string">&quot;buf %p val %u&quot;</span>, buf, val);</span><br><span class="line">	<span class="comment">//大小端转换，cpu本身就是小端模式，所以这步什么都不做。</span></span><br><span class="line">	val = sys_cpu_to_le32(val);</span><br><span class="line">    <span class="comment">//从尾指针拷贝要放入的数据（net_buf_simple_add 事先已经把剩余长度给改变了）</span></span><br><span class="line">	<span class="built_in">memcpy</span>(net_buf_simple_add(buf, <span class="keyword">sizeof</span>(val)), &amp;val, <span class="keyword">sizeof</span>(val));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//尾部添加大端32位数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">net_buf_simple_add_be32</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">u32_t</span> val)</span></span><br><span class="line">&#123;</span><br><span class="line">	NET_BUF_SIMPLE_DBG(<span class="string">&quot;buf %p val %u&quot;</span>, buf, val);</span><br><span class="line">	<span class="comment">//大小端转换，cpu本身就是小端模式，所以这步要把4个字节颠倒存储。</span></span><br><span class="line">	val = sys_cpu_to_be32(val);</span><br><span class="line">    <span class="comment">//从尾指针拷贝要放入的数据（net_buf_simple_add 事先已经把剩余长度给改变了）</span></span><br><span class="line">	<span class="built_in">memcpy</span>(net_buf_simple_add(buf, <span class="keyword">sizeof</span>(val)), &amp;val, <span class="keyword">sizeof</span>(val));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//头部追加（只增加剩余长度和修改了指针指向，实际未添加数据）</span></span><br><span class="line"><span class="type">void</span> *<span class="title function_">net_buf_simple_push</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">size_t</span> len)</span></span><br><span class="line">&#123;</span><br><span class="line">	NET_BUF_SIMPLE_DBG(<span class="string">&quot;buf %p len %zu&quot;</span>, buf, len);</span><br><span class="line"></span><br><span class="line">	NET_BUF_SIMPLE_ASSERT(net_buf_simple_headroom(buf) &gt;= len);</span><br><span class="line"></span><br><span class="line">	buf-&gt;data -= len;	<span class="comment">//当前指针指向向前偏移</span></span><br><span class="line">	buf-&gt;len += len;	<span class="comment">//增加剩余数据长度</span></span><br><span class="line">	<span class="keyword">return</span> buf-&gt;data;	<span class="comment">//返回当前指针指向地址</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//头部添加小端16位数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">net_buf_simple_push_le16</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">u16_t</span> val)</span></span><br><span class="line">&#123;</span><br><span class="line">	NET_BUF_SIMPLE_DBG(<span class="string">&quot;buf %p val %u&quot;</span>, buf, val);</span><br><span class="line"></span><br><span class="line">	val = sys_cpu_to_le16(val);</span><br><span class="line">	<span class="built_in">memcpy</span>(net_buf_simple_push(buf, <span class="keyword">sizeof</span>(val)), &amp;val, <span class="keyword">sizeof</span>(val));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//头部添加大端16位数据</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">net_buf_simple_push_be16</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">u16_t</span> val)</span></span><br><span class="line">&#123;</span><br><span class="line">	NET_BUF_SIMPLE_DBG(<span class="string">&quot;buf %p val %u&quot;</span>, buf, val);</span><br><span class="line"></span><br><span class="line">	val = sys_cpu_to_be16(val);</span><br><span class="line">	<span class="built_in">memcpy</span>(net_buf_simple_push(buf, <span class="keyword">sizeof</span>(val)), &amp;val, <span class="keyword">sizeof</span>(val));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//缓存区头部偏移操作</span></span><br><span class="line"><span class="type">void</span> *<span class="title function_">net_buf_simple_pull</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">size_t</span> len)</span></span><br><span class="line">&#123;</span><br><span class="line">	NET_BUF_SIMPLE_DBG(<span class="string">&quot;buf %p len %zu&quot;</span>, buf, len);</span><br><span class="line"></span><br><span class="line">	NET_BUF_SIMPLE_ASSERT(buf-&gt;len &gt;= len);</span><br><span class="line"></span><br><span class="line">	buf-&gt;len -= len;			<span class="comment">//减少剩余数据长度</span></span><br><span class="line">	<span class="keyword">return</span> buf-&gt;data += len;	<span class="comment">//当前指针指向向后偏移</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//头部读取小端16位数据</span></span><br><span class="line"><span class="type">u16_t</span> <span class="title function_">net_buf_simple_pull_le16</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">u16_t</span> val;</span><br><span class="line">	<span class="comment">//读取16位数据</span></span><br><span class="line">	val = UNALIGNED_GET((<span class="type">u16_t</span> *)buf-&gt;data);</span><br><span class="line">    <span class="comment">//缓存区头部指针偏移16位</span></span><br><span class="line">	net_buf_simple_pull(buf, <span class="keyword">sizeof</span>(val));</span><br><span class="line">	<span class="comment">//根据系统大小端转换</span></span><br><span class="line">	<span class="keyword">return</span> sys_le16_to_cpu(val);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//头部读取大端16位数据</span></span><br><span class="line"><span class="type">u16_t</span> <span class="title function_">net_buf_simple_pull_be16</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">u16_t</span> val;</span><br><span class="line">	<span class="comment">//读取16位数据</span></span><br><span class="line">	val = UNALIGNED_GET((<span class="type">u16_t</span> *)buf-&gt;data);</span><br><span class="line">    <span class="comment">//缓存区头部指针偏移16位</span></span><br><span class="line">	net_buf_simple_pull(buf, <span class="keyword">sizeof</span>(val));</span><br><span class="line">	<span class="comment">//根据系统大小端转换</span></span><br><span class="line">	<span class="keyword">return</span> sys_be16_to_cpu(val);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="二、配网器功能分析"><a href="#二、配网器功能分析" class="headerlink" title="二、配网器功能分析"></a><font size=3>二、配网器功能分析</font></h1><p>我们先来分析一下配网器需要实现哪些功能，我们下载的阿里提供的天猫精灵开发SDK使用的蓝牙Mesh协议栈是没有给我们写配网器的相关实现的，我们需要添加代码自己实现配网器功能，那配网器都需要实现哪些功能？我们简单分析一下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240107095929928.png" alt="image-20240107095929928" />

<p>我们前面用手机给设备配网的时候看到，配网器需要自己生成一些密钥，例如网络密钥，app密钥等。我们还需要为每个元素分配地址，一般来说0x0001分配给配网器，后面累加的地址分配给元素。我们配网完成，连接好之后，需要存储一些节点的信息，例如节点的MAC地址，MAC地址通常会以UUID的格式存储在配网器中，这样若是某个设备断网，重新连接过来的时候，我们的配网器可以把之前分配给它的地址重新分配给它。具体的可以看后面的代码实现。</p>
<p>大概要有两个阶段：</p>
<ul>
<li><p>配网阶段：为设备分配单播地址、添加网络密钥(NetKey)等。通过配网，设备加入 BLE-MESH 网络，身份从未配网设备变为节点。</p>
</li>
<li><p>配置阶段：为节点添加应用密钥(AppKey),并将应用密钥绑定到相应模型。配置期间，有些选项是可选的，比如为节点添加订阅地址、设置发布地址等。通过配置，该节点实际上可以向Provisioner 发送消息，也可以接收来自Provisioner的消息。</p>
</li>
</ul>
<h1 id="三、配网器实现"><a href="#三、配网器实现" class="headerlink" title="三、配网器实现"></a><font size=3>三、配网器实现</font></h1><p>完整的修改可以看这里：<a target="_blank" rel="noopener" href="https://github.com/sumumm/my-genie-bt-mesh-stack/commit/9e0c6e701bf31d6f02f88018e4212cf4ba706d19">feat:bt_provisioner工程实现配网器(支持配网器和节点的编译) · sumumm&#x2F;my-genie-bt-mesh-stack@9e0c6e7 (github.com)</a></p>
<h2 id="1-工程修改"><a href="#1-工程修改" class="headerlink" title="1. 工程修改"></a><font size=3>1. 工程修改</font></h2><h3 id="1-1-helloworld-mk"><a href="#1-1-helloworld-mk" class="headerlink" title="1.1 helloworld.mk"></a><font size=3>1.1 helloworld.mk</font></h3><p>①、以helloworld工程为示例，修改helloworld.mk</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line">NAME := helloworld</span><br><span class="line"></span><br><span class="line"><span class="variable">$(NAME)</span>_COMPONENTS  += genie_app bluetooth.bt_host bluetooth.bt_mesh yloop cli</span><br><span class="line"></span><br><span class="line"><span class="variable">$(NAME)</span>_INCLUDES += ../../../../genie_app \</span><br><span class="line">					../../../../genie_app/base \</span><br><span class="line">					../../../../genie_app/bluetooth/host \</span><br><span class="line">					../../../../genie_app/bluetooth/mesh</span><br><span class="line"></span><br><span class="line"><span class="variable">$(NAME)</span>_SOURCES  := helloworld.c</span><br><span class="line"></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(HOST_MCU_FAMILY)</span>,ch6121)</span><br><span class="line">bt_host_tinycrypt = 0</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">ifeq</span> (<span class="variable">$(HOST_MCU_FAMILY)</span>,tg7100b)</span><br><span class="line">bt_host_tinycrypt = 0</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">ble = 1</span><br><span class="line">bt_mesh = 1</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line">en_bt_smp = 1</span><br><span class="line"></span><br><span class="line"><span class="comment"># Host configurations</span></span><br><span class="line">GLOBAL_DEFINES += CONFIG_BLUETOOTH</span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(HOST_MCU_FAMILY)</span>,ch6121)</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_RX_BUF_LEN=252</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_L2CAP_TX_MTU=244</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_ADV_BLOCK</span><br><span class="line"><span class="keyword">else</span> <span class="keyword">ifeq</span> (<span class="variable">$(HOST_MCU_FAMILY)</span>,tg7100b)</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_RX_BUF_LEN=252</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_L2CAP_TX_MTU=244</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_ADV_BLOCK</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_SMP</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_HCI_VS_EXT</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_TINYCRYPT_ECC</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_CONN</span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BT_CENTRAL</span></span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_PERIPHERAL</span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BLE_50</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Mesh function select</span></span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_PROV</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_PB_ADV</span><br><span class="line"><span class="comment">#关闭GATT代理（为了实现简单配网，减少开发难度）</span></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BT_MESH_PB_GATT</span></span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_PROXY</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_GATT_PROXY</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_RELAY</span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BT_MESH_FRIEND</span></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BT_MESH_LOW_POWER</span></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BT_MESH_SHELL</span></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BT_MESH_BQB</span></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_BT_MESH_IV_UPDATE_TEST</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Mesh foundation model select</span></span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_CFG_SRV</span><br><span class="line">GLOBAL_DEFINES += CONFIG_BT_MESH_HEALTH_SRV</span><br><span class="line"></span><br><span class="line"><span class="comment"># Mesh debug message enable</span></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += USE_BT_MESH_CUSTOM_LOG</span></span><br><span class="line">GLOBAL_DEFINES += GENIE_DEBUG_COLOR</span><br><span class="line">GLOBAL_DEFINES += MESH_DEBUG_RX</span><br><span class="line">GLOBAL_DEFINES += MESH_DEBUG_TX</span><br><span class="line">GLOBAL_DEFINES += MESH_DEBUG_PROV</span><br><span class="line"></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_INIT_STACKS</span></span><br><span class="line"><span class="comment">#GLOBAL_DEFINES += CONFIG_PRINTK</span></span><br><span class="line"></span><br><span class="line">GLOBAL_INCLUDES += ../</span><br><span class="line"></span><br><span class="line">GLOBAL_DEFINES += PROJECT_SW_VERSION=0x00010000</span><br><span class="line">GLOBAL_DEFINES += PROJECT_SECRET_KEY=\<span class="string">&quot;00112233445566770011223344556677\&quot;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">MESH_MODEL_GEN_ONOFF_SRV = 1</span></span><br><span class="line"><span class="string">MESH_MODEL_DIABLE_TRANS = 1</span></span><br><span class="line"><span class="string">#MESH_MODEL_LIGHTNESS_SRV = 1</span></span><br><span class="line"><span class="string">#MESH_MODEL_CTL_SRV = 1</span></span><br><span class="line"><span class="string">#MESH_MODEL_VENDOR_SRV = 1</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">ALI_SIMPLE_MODLE = 1</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">GLOBAL_DEFINES += GENIE_OLD_AUTH</span></span><br><span class="line"><span class="string">#定义一个配网器角色的宏，以便和未配网节点代码分开</span></span><br><span class="line"><span class="string">GLOBAL_DEFINES += CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line"><span class="string">#开启DEBUG打印宏，方便调试</span></span><br><span class="line"><span class="string">GLOBAL_DEFINES += CONFIG_BT_DEBUG_LOG</span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="1-2-bt-mesh-mk"><a href="#1-2-bt-mesh-mk" class="headerlink" title="1.2 bt_mesh.mk"></a><font size=3>1.2 bt_mesh.mk</font></h3><p>②、修改  genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;bt_mesh.mk</p>
<p>将：<code>./src/prov.c \</code>替换成<code>./src/provisioner_prov.c \</code></p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">NAME := bt_mesh</span><br><span class="line"></span><br><span class="line"><span class="variable">$(NAME)</span>_MBINS_TYPE := kernel</span><br><span class="line"><span class="variable">$(NAME)</span>_VERSION := 1.0.0</span><br><span class="line"><span class="variable">$(NAME)</span>_SUMMARY := BLE Mesh stack.</span><br><span class="line"></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(bt_mesh_standalone_deploy)</span>,1)</span><br><span class="line">GLOBAL_DEFINES += CONFIG_MESH_STACK_ALONE</span><br><span class="line"><span class="variable">$(NAME)</span>_COMPONENTS += bluetooth.bt_mesh.util</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"><span class="variable">$(NAME)</span>_COMPONENTS += bluetooth.bt_common </span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(NAME)</span>_COMPONENTS += bluetooth.bt_mesh.ref_impl</span><br><span class="line"></span><br><span class="line"><span class="variable">$(NAME)</span>_INCLUDES += ./inc/ \</span><br><span class="line">                    ./inc/api/ \</span><br><span class="line">                    ./inc/api/mesh/ \</span><br><span class="line">                    ../../../genie_app/ \</span><br><span class="line">					../../../genie_app/base \</span><br><span class="line">					../../../genie_app/bluetooth/host \</span><br><span class="line">					../../../genie_app/bluetooth/mesh</span><br><span class="line"></span><br><span class="line">                    </span><br><span class="line"><span class="variable">$(NAME)</span>_SOURCES  :=  ./src/access.c \</span><br><span class="line">                     ./src/adv.c \</span><br><span class="line">                     ./src/beacon.c \</span><br><span class="line">                     ./src/crypto.c \</span><br><span class="line">                     ./src/cfg_srv.c \</span><br><span class="line">                     ./src/cfg_cli.c \</span><br><span class="line">                     ./src/health_srv.c \</span><br><span class="line">                     ./src/health_cli.c \</span><br><span class="line">                     ./src/main.c \</span><br><span class="line">                     ./src/net.c \</span><br><span class="line">                     ./src/provisioner_prov.c \</span><br><span class="line">                     ./src/proxy.c \</span><br><span class="line">                     ./src/transport.c \</span><br><span class="line">                     ./src/friend.c \</span><br><span class="line">                     ./src/lpn.c \</span><br><span class="line">                     ./src/shell.c </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">GLOBAL_INCLUDES += ./inc/ \</span><br><span class="line">                   ./inc/api</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">GLOBAL_DEFINES += CRC16_ENABLED</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="2-初始化修改部分文件"><a href="#2-初始化修改部分文件" class="headerlink" title="2. 初始化修改部分文件"></a><font size=3>2. 初始化修改部分文件</font></h2><p>这部分修改的都是协议栈的源码，需要注意用宏控制好。</p>
<h3 id="2-1-net-h"><a href="#2-1-net-h" class="headerlink" title="2.1 net.h"></a><font size=3>2.1 net.h</font></h3><p>①、文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;inc&#x2F;net.h</p>
<p>添加配网器代码：定义宏 CONFIG_BT_MESH_ROLE_PROVISIONER</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight h"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//新增定义</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT	3</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT	3</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_net</span> &#123;</span></span><br><span class="line">	<span class="type">u32_t</span> iv_index;          <span class="comment">/* Current IV Index */</span></span><br><span class="line">	<span class="type">u32_t</span> seq:<span class="number">24</span>,            <span class="comment">/* Next outgoing sequence number */</span></span><br><span class="line">	      iv_update:<span class="number">1</span>,       <span class="comment">/* 1 if IV Update in Progress */</span></span><br><span class="line">	      ivu_initiator:<span class="number">1</span>,   <span class="comment">/* IV Update initiated by us */</span></span><br><span class="line">	      ivu_test:<span class="number">1</span>,        <span class="comment">/* IV Update test mode */</span></span><br><span class="line">	      pending_update:<span class="number">1</span>,  <span class="comment">/* Update blocked by SDU in progress */</span></span><br><span class="line">	      valid:<span class="number">1</span>;           <span class="comment">/* 0 if unused */</span></span><br><span class="line"></span><br><span class="line">	<span class="type">s64_t</span> last_update;       <span class="comment">/* Time since last IV Update change */</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">/* Local network interface */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">k_work</span> <span class="title">local_work</span>;</span></span><br><span class="line">	<span class="type">sys_slist_t</span> local_queue;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_FRIEND)</span></span><br><span class="line">	<span class="comment">/* Friend state, unique for each LPN that we&#x27;re Friends for */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_friend</span> <span class="title">frnd</span>[<span class="title">CONFIG_BT_MESH_FRIEND_LPN_COUNT</span>];</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_LOW_POWER)</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_lpn</span> <span class="title">lpn</span>;</span>  <span class="comment">/* Low Power Node state */</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">	<span class="comment">/* Timer to transition IV Update in Progress state */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">k_delayed_work</span> <span class="title">ivu_complete</span>;</span></span><br><span class="line"></span><br><span class="line">	<span class="type">u8_t</span> dev_key[<span class="number">16</span>];</span><br><span class="line"></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_app_key</span> <span class="title">app_keys</span>[<span class="title">CONFIG_BT_MESH_APP_KEY_COUNT</span>];</span></span><br><span class="line"></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_subnet</span> <span class="title">sub</span>[<span class="title">CONFIG_BT_MESH_SUBNET_COUNT</span>];</span></span><br><span class="line"></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_rpl</span> <span class="title">rpl</span>[<span class="title">CONFIG_BT_MESH_CRPL</span>];</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line">    <span class="comment">/* Application keys stored by provisioner */</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_app_key</span> *<span class="title">p_app_keys</span>[<span class="title">CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT</span>];</span></span><br><span class="line">    <span class="comment">/* Next app_idx can be assigned */</span></span><br><span class="line">    <span class="type">uint16_t</span> p_app_idx_next;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Network keys stored by provisioner */</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_subnet</span> *<span class="title">p_sub</span>[<span class="title">CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT</span>];</span></span><br><span class="line">    <span class="comment">/* Next net_idx can be assigned */</span></span><br><span class="line">    <span class="type">uint16_t</span> p_net_idx_next;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-2-main-h"><a href="#2-2-main-h" class="headerlink" title="2.2 main.h"></a><font size=3>2.2 main.h</font></h3><p>②、文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;inc&#x2F;api&#x2F;mesh&#x2F;main.h</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_prov</span> &#123;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_NODE</span></span><br><span class="line">    <span class="type">const</span> <span class="type">u8_t</span> *uuid;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *uri;</span><br><span class="line">    <span class="type">bt_mesh_prov_oob_info_t</span> oob_info;</span><br><span class="line">    <span class="type">u8_t</span>        public_key_type;</span><br><span class="line">    <span class="type">u8_t</span> *		static_val;</span><br><span class="line">    <span class="type">u8_t</span>        static_val_len;</span><br><span class="line"></span><br><span class="line">    <span class="type">u8_t</span>        output_size;</span><br><span class="line">    <span class="type">u16_t</span>       output_actions;</span><br><span class="line"></span><br><span class="line">    <span class="type">u8_t</span>        input_size;</span><br><span class="line">    <span class="type">u16_t</span>       input_actions;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="type">int</span>         (*output_number)(<span class="type">bt_mesh_output_action_t</span> act, <span class="type">u32_t</span> num);</span><br><span class="line">    <span class="type">int</span>         (*output_string)(<span class="type">const</span> <span class="type">char</span> *str);</span><br><span class="line">    <span class="type">int</span>         (*input)(<span class="type">bt_mesh_input_action_t</span> act, <span class="type">u8_t</span> size);</span><br><span class="line">    <span class="type">void</span>        (*link_open)(<span class="type">bt_mesh_prov_bearer_t</span> bearer);</span><br><span class="line">    <span class="type">void</span>        (*link_close)(<span class="type">bt_mesh_prov_bearer_t</span> bearer);</span><br><span class="line">    <span class="type">void</span>        (*complete)(<span class="type">u16_t</span> net_idx, <span class="type">u16_t</span> addr);</span><br><span class="line">    <span class="type">void</span>        (*reset)(<span class="type">void</span>);</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BLE_MESH_NODE */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> <span class="type">uint8_t</span> *uuid;</span><br><span class="line">    <span class="type">const</span> <span class="type">char</span> *uri;</span><br><span class="line"></span><br><span class="line">    <span class="type">bt_mesh_prov_oob_info_t</span> oob_info;</span><br><span class="line">    <span class="type">u8_t</span>        public_key_type;</span><br><span class="line">    </span><br><span class="line">    <span class="type">u8_t</span> *		static_val;</span><br><span class="line">    <span class="type">u8_t</span>        static_val_len;</span><br><span class="line">    <span class="type">u8_t</span>        output_size;</span><br><span class="line">    <span class="type">u16_t</span>       output_actions;</span><br><span class="line">    <span class="type">u8_t</span>        input_size;</span><br><span class="line">    <span class="type">u16_t</span>       input_actions;</span><br><span class="line">    </span><br><span class="line">    <span class="type">uint16_t</span> prov_unicast_addr;</span><br><span class="line">    <span class="type">uint16_t</span> prov_start_address;</span><br><span class="line">    <span class="type">uint8_t</span>  prov_attention;</span><br><span class="line">    <span class="type">uint8_t</span>  prov_algorithm;</span><br><span class="line"></span><br><span class="line">    <span class="type">uint8_t</span>  prov_pub_key_oob;</span><br><span class="line">    <span class="type">uint8_t</span>  flags;</span><br><span class="line">    <span class="type">uint32_t</span> iv_index;</span><br><span class="line">    </span><br><span class="line">    <span class="type">int</span>  (*prov_pub_key_oob_cb)(<span class="type">void</span>);</span><br><span class="line">    <span class="type">int</span>  (*prov_input_num)(<span class="type">uint8_t</span> method, <span class="type">bt_mesh_output_action_t</span> act, <span class="type">uint8_t</span> size, <span class="type">uint8_t</span> link_idx);</span><br><span class="line">    <span class="type">int</span>  (*prov_output_num)(<span class="type">uint8_t</span> method, <span class="type">bt_mesh_input_action_t</span> act, <span class="type">void</span> *data, <span class="type">uint8_t</span> size, <span class="type">uint8_t</span> link_idx);</span><br><span class="line"></span><br><span class="line">    <span class="type">void</span> (*link_open)(<span class="type">bt_mesh_prov_bearer_t</span> bearer);</span><br><span class="line">    <span class="type">void</span> (*link_close)(<span class="type">bt_mesh_prov_bearer_t</span> bearer);</span><br><span class="line">    <span class="type">void</span> (*complete)(<span class="type">uint16_t</span> node_idx,<span class="type">uint16_t</span> unicast_addr);</span><br><span class="line">    <span class="type">void</span>        (*reset)(<span class="type">void</span>);                      </span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BT_MESH_ROLE_PROVISIONER */</span></span></span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-3-crypto-c"><a href="#2-3-crypto-c" class="headerlink" title="2.3 crypto.c"></a><font size=3>2.3 crypto.c</font></h3><p>③、文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;src&#x2F;crypto.c</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//新添加</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_prov_encrypt</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> key[<span class="number">16</span>], <span class="type">uint8_t</span> nonce[<span class="number">13</span>],</span></span><br><span class="line"><span class="params">                         <span class="type">const</span> <span class="type">uint8_t</span> data[<span class="number">25</span>], <span class="type">uint8_t</span> out[<span class="number">33</span>])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> bt_mesh_ccm_encrypt(key, nonce, data, <span class="number">25</span>, <span class="literal">NULL</span>, <span class="number">0</span>, out, <span class="number">8</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;inc&#x2F;mesh_crypto.h</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//新增</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_prov_encrypt</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> key[<span class="number">16</span>], <span class="type">uint8_t</span> nonce[<span class="number">13</span>],</span></span><br><span class="line"><span class="params">                         <span class="type">const</span> <span class="type">uint8_t</span> data[<span class="number">25</span>], <span class="type">uint8_t</span> out[<span class="number">33</span>])</span>;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-4-adv-c"><a href="#2-4-adv-c" class="headerlink" title="2.4 adv.c"></a><font size=3>2.4 adv.c</font></h3><p>④、文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;src&#x2F;adv.c</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <p>在  bt_mesh_scan_cb()  函数里修改：</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">if</span> (adv_type == BT_LE_ADV_NONCONN_IND) &#123;</span><br><span class="line">        <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">            <span class="keyword">case</span> BT_MESH_DATA_MESH_MESSAGE:</span><br><span class="line">                bt_mesh_net_recv(buf, rssi, BT_MESH_NET_IF_ADV);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line">            <span class="keyword">case</span> BT_MESH_DATA_MESH_PROV:</span><br><span class="line">            <span class="comment">//配网器接收</span></span><br><span class="line">            <span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line">                </span><br><span class="line">                bt_mesh_prov_adv_recv(buf);</span><br><span class="line"></span><br><span class="line">            <span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">            <span class="comment">//节点接收</span></span><br><span class="line">            <span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_NODE</span></span><br><span class="line">                bt_mesh_pb_adv_recv(buf);</span><br><span class="line"></span><br><span class="line">            <span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">         </span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-5-beacon-c"><a href="#2-5-beacon-c" class="headerlink" title="2.5 beacon.c"></a><font size=3>2.5 beacon.c</font></h3><p>⑤、文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;src&#x2F;beacon.c</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <p>在  bt_mesh_beacon_recv()  函数里修改：</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//添加</span></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span>  CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;provisioner_prov.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">bt_mesh_beacon_recv</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">u8_t</span> type;</span><br><span class="line">    <span class="comment">//BT_DBG(&quot;%u bytes: %s&quot;, buf-&gt;len, bt_hex(buf-&gt;data, buf-&gt;len));</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (buf-&gt;len &lt; <span class="number">1</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Too short beacon&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    type = net_buf_simple_pull_u8(buf);</span><br><span class="line">    <span class="keyword">switch</span> (type) &#123;</span><br><span class="line">        <span class="keyword">case</span> BEACON_TYPE_UNPROVISIONED:</span><br><span class="line">            BT_DBG(<span class="string">&quot;Ignoring unprovisioned device beacon&quot;</span>);</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line">            <span class="keyword">extern</span> <span class="type">void</span> <span class="title function_">provisioner_link_open</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> * uuid)</span>;</span><br><span class="line">            <span class="comment">//发起配网</span></span><br><span class="line">            <span class="keyword">if</span>(is_provisioning() == <span class="number">0</span>)</span><br><span class="line">            &#123;</span><br><span class="line">                    provisioner_link_open(buf-&gt;data);    </span><br><span class="line">            &#125;               </span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> BEACON_TYPE_SECURE:</span><br><span class="line">            secure_beacon_recv(buf);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            BT_WARN(<span class="string">&quot;Unknown beacon type 0x%02x&quot;</span>, type);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_NODE		<span class="comment">//配网器不需要这个函数</span></span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">unprovisioned_beacon_send</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line">    <span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_prov</span> *<span class="title">prov</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span>;</span></span><br><span class="line">    <span class="type">u8_t</span> uri_hash[<span class="number">16</span>] = &#123; <span class="number">0</span> &#125;;</span><br><span class="line">    <span class="type">u16_t</span> oob_info;</span><br><span class="line"></span><br><span class="line">    BT_DBG(<span class="string">&quot;&quot;</span>);</span><br><span class="line"></span><br><span class="line">    buf = bt_mesh_adv_create(BT_MESH_ADV_BEACON, UNPROV_XMIT_COUNT,</span><br><span class="line">                             UNPROV_XMIT_INT, K_NO_WAIT);</span><br><span class="line">    <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Unable to allocate beacon buffer&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> -ENOBUFS;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    prov = bt_mesh_prov_get();</span><br><span class="line"></span><br><span class="line">    net_buf_add_u8(buf, BEACON_TYPE_UNPROVISIONED);</span><br><span class="line">    net_buf_add_mem(buf, prov-&gt;uuid, <span class="number">16</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* for tmall ble profile, OOB info default is 0x0000</span></span><br><span class="line"><span class="comment">       URI hash is optional */</span></span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;uri &amp;&amp; bt_mesh_s1(prov-&gt;uri, uri_hash) == <span class="number">0</span>) &#123;</span><br><span class="line">        oob_info = prov-&gt;oob_info | BT_MESH_PROV_OOB_URI;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        oob_info = prov-&gt;oob_info;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    net_buf_add_be16(buf, oob_info);</span><br><span class="line">    net_buf_add_mem(buf, uri_hash, <span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">    bt_mesh_adv_send(buf, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">    net_buf_unref(buf);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;uri) &#123;</span><br><span class="line">        <span class="type">size_t</span> len;</span><br><span class="line"></span><br><span class="line">        buf = bt_mesh_adv_create(BT_MESH_ADV_URI, UNPROV_XMIT_COUNT,</span><br><span class="line">                                 UNPROV_XMIT_INT, K_NO_WAIT);</span><br><span class="line">        <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Unable to allocate URI buffer&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> -ENOBUFS;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        len = <span class="built_in">strlen</span>(prov-&gt;uri);</span><br><span class="line">        <span class="keyword">if</span> (net_buf_tailroom(buf) &lt; len) &#123;</span><br><span class="line">            BT_WARN(<span class="string">&quot;Too long URI to fit advertising data&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            net_buf_add_mem(buf, prov-&gt;uri, len);</span><br><span class="line">            bt_mesh_adv_send(buf, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        net_buf_unref(buf);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BT_MESH_PB_ADV */</span></span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">beacon_send</span><span class="params">(<span class="keyword">struct</span> k_work *work)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* Don&#x27;t send anything if we have an active provisioning link */</span></span><br><span class="line">    <span class="keyword">if</span> (IS_ENABLED(CONFIG_BT_MESH_PROV) &amp;&amp; bt_prov_active()) &#123;</span><br><span class="line">        k_delayed_work_submit(&amp;beacon_timer, unprov_beacon_interval_get());</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//BT_DBG(&quot;&quot;);</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_is_provisioned()) &#123;</span><br><span class="line">        update_beacon_observation();</span><br><span class="line">        secure_beacon_send();</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Only resubmit if beaconing is still enabled */</span></span><br><span class="line">        <span class="keyword">if</span> (bt_mesh_beacon_get() == BT_MESH_BEACON_ENABLED ||</span><br><span class="line">            bt_mesh.ivu_initiator) &#123;</span><br><span class="line">            k_delayed_work_submit(&amp;beacon_timer,</span><br><span class="line">                                  PROVISIONED_INTERVAL);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_NODE</span></span><br><span class="line">        unprovisioned_beacon_send();		<span class="comment">//屏蔽</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">        k_delayed_work_submit(&amp;beacon_timer, unprov_beacon_interval_get());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-6-main-c"><a href="#2-6-main-c" class="headerlink" title="2.6 main.c"></a><font size=3>2.6 main.c</font></h3><p>⑥、文件路径：genie-bt-mesh-stack-master&#x2F;network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;src&#x2F;main.c</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <p>添加头文件：</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#ifdef CONFIG_BT_MESH_ROLE_PROVISIONER</span><br><span class="line">#include &quot;provisioner_prov.h&quot;</span><br><span class="line">#endif</span><br></pre></td></tr></table></figure><p>在 bt_mesh_init()函数里添加：</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_init</span><span class="params">(<span class="type">const</span> <span class="keyword">struct</span> bt_mesh_prov *prov,</span></span><br><span class="line"><span class="params">                    <span class="type">const</span> <span class="keyword">struct</span> bt_mesh_comp *comp)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> err;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_MESH_STACK_ALONE</span></span><br><span class="line">        bt_mesh_prepare();</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    err = bt_mesh_test();</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        <span class="keyword">return</span> err;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//注册成分数据</span></span><br><span class="line">    err = bt_mesh_comp_register(comp);</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        <span class="keyword">return</span> err;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//初始化配网信息</span></span><br><span class="line">    <span class="keyword">if</span> (IS_ENABLED(CONFIG_BT_MESH_PROV)) &#123;</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_NODE</span></span><br><span class="line">        err = bt_mesh_prov_init(prov);		<span class="comment">//节点设备初始化</span></span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            <span class="keyword">return</span> err;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    &#125;   </span><br><span class="line"></span><br><span class="line">    <span class="comment">//网络层初始化</span></span><br><span class="line">    bt_mesh_net_init();</span><br><span class="line">    <span class="comment">//传输层初始化</span></span><br><span class="line">    bt_mesh_trans_init();</span><br><span class="line">    <span class="comment">//承载层初始化</span></span><br><span class="line">    bt_mesh_beacon_init();</span><br><span class="line">    <span class="comment">//广播初始化</span></span><br><span class="line">    bt_mesh_adv_init();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//加些延时,需要给获得密钥一些时间</span></span><br><span class="line">    aos_msleep(<span class="number">3000</span>);</span><br><span class="line">    <span class="keyword">if</span> (IS_ENABLED(CONFIG_BT_MESH_PROV)) &#123;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line">        err = bt_mesh_provisioner_prov_init(prov);		<span class="comment">//配网器初始化</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            <span class="keyword">return</span> err;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    &#125; </span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (IS_ENABLED(CONFIG_BT_MESH_GATT_PROXY)) &#123;</span><br><span class="line">        bt_mesh_proxy_init();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在 bt_mesh_provision() 函数里添加宏：</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">if</span> (IS_ENABLED(CONFIG_BT_MESH_PROV)) &#123;</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_NODE     			<span class="comment">//添加宏 </span></span></span><br><span class="line">        bt_mesh_prov_complete(net_idx, addr);	<span class="comment">//配网器不运行下边函数 </span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-7-genie-mesh-c"><a href="#2-7-genie-mesh-c" class="headerlink" title="2.7 genie_mesh.c"></a><font size=3>2.7 genie_mesh.c</font></h3><p>⑦、文件路径：genie-bt-mesh-stack-master&#x2F;genie_app&#x2F;bluetooth&#x2F;mesh&#x2F;genie_mesh.c</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <p>在 genie_mesh_init() 函数里</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">genie_mesh_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> ret;</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;&gt;&gt;&gt;init genie&lt;&lt;&lt;&quot;</span>);</span><br><span class="line">    <span class="comment">//加载三元组</span></span><br><span class="line">    genie_tri_tuple_load();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//发送配网广播的数据</span></span><br><span class="line">    prov.uuid = genie_tri_tuple_get_uuid();</span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> GENIE_OLD_AUTH</span></span><br><span class="line">    prov.static_val = genie_tri_tuple_get_auth();</span><br><span class="line">    prov.static_val_len = STATIC_OOB_LENGTH;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    <span class="comment">//无OOB认证</span></span><br><span class="line">    prov.static_val = <span class="literal">NULL</span>;</span><br><span class="line">    prov.static_val_len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line">	<span class="comment">//配网器单播地址0x0001、未配网节点分配的起始地址0x0005</span></span><br><span class="line">    prov.prov_unicast_addr = <span class="number">0x0001</span>;</span><br><span class="line">    prov.prov_start_address = <span class="number">0x0005</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    prov.complete = _prov_complete;</span><br><span class="line">    prov.reset = _prov_reset;</span><br><span class="line">    <span class="comment">//成分数据初始化</span></span><br><span class="line">    comp.cid = CONFIG_CID_TAOBAO;</span><br><span class="line">    comp.pid = <span class="number">0</span>;</span><br><span class="line">    comp.vid = <span class="number">1</span>; <span class="comment">// firmware version fir ota</span></span><br><span class="line">    comp.elem = elements;</span><br><span class="line">    comp.elem_count = get_vendor_element_num();</span><br><span class="line">    <span class="comment">//HCI驱动初始化     蓝牙主机  &lt;-&gt; 蓝牙控制器</span></span><br><span class="line">    hci_driver_init();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//开启蓝牙</span></span><br><span class="line">    ret = bt_enable(_genie_mesh_ready);</span><br><span class="line">    <span class="keyword">if</span> (ret) &#123;</span><br><span class="line">        BT_INFO(<span class="string">&quot;init err %d&quot;</span>, ret);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="3-配网器配网过程部分实现"><a href="#3-配网器配网过程部分实现" class="headerlink" title="3. 配网器配网过程部分实现"></a><font size=3>3. 配网器配网过程部分实现</font></h2><h3 id="3-1-provisioner-prov-h"><a href="#3-1-provisioner-prov-h" class="headerlink" title="3.1 provisioner_prov.h"></a><font size=3>3.1 provisioner_prov.h</font></h3><p>配网器.h文件我们需要新建一个，放在这个目录下：network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;inc&#x2F;provisioner_prov.h。</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifndef</span> PROVISIONER_PROV_H</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROVISIONER_PROV_H</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;net/buf.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;mesh.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">provisioner_link_open</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> * uuid)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">is_provisioning</span><span class="params">(<span class="type">void</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_prov_active</span><span class="params">(<span class="type">void</span>)</span>;</span><br><span class="line"><span class="type">void</span> <span class="title function_">bt_mesh_prov_adv_recv</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_provisioner_prov_init</span><span class="params">(<span class="type">const</span> <span class="keyword">struct</span> bt_mesh_prov *prov_info)</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2-provisioner-prov-c"><a href="#3-2-provisioner-prov-c" class="headerlink" title="3.2 provisioner_prov.c"></a><font size=3>3.2 provisioner_prov.c</font></h3><p>配网器.c文件我们也需要新建，放在这个目录下：network&#x2F;bluetooth&#x2F;bt_mesh&#x2F;src&#x2F;provisioner_prov.c。</p>
<details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br><span class="line">525</span><br><span class="line">526</span><br><span class="line">527</span><br><span class="line">528</span><br><span class="line">529</span><br><span class="line">530</span><br><span class="line">531</span><br><span class="line">532</span><br><span class="line">533</span><br><span class="line">534</span><br><span class="line">535</span><br><span class="line">536</span><br><span class="line">537</span><br><span class="line">538</span><br><span class="line">539</span><br><span class="line">540</span><br><span class="line">541</span><br><span class="line">542</span><br><span class="line">543</span><br><span class="line">544</span><br><span class="line">545</span><br><span class="line">546</span><br><span class="line">547</span><br><span class="line">548</span><br><span class="line">549</span><br><span class="line">550</span><br><span class="line">551</span><br><span class="line">552</span><br><span class="line">553</span><br><span class="line">554</span><br><span class="line">555</span><br><span class="line">556</span><br><span class="line">557</span><br><span class="line">558</span><br><span class="line">559</span><br><span class="line">560</span><br><span class="line">561</span><br><span class="line">562</span><br><span class="line">563</span><br><span class="line">564</span><br><span class="line">565</span><br><span class="line">566</span><br><span class="line">567</span><br><span class="line">568</span><br><span class="line">569</span><br><span class="line">570</span><br><span class="line">571</span><br><span class="line">572</span><br><span class="line">573</span><br><span class="line">574</span><br><span class="line">575</span><br><span class="line">576</span><br><span class="line">577</span><br><span class="line">578</span><br><span class="line">579</span><br><span class="line">580</span><br><span class="line">581</span><br><span class="line">582</span><br><span class="line">583</span><br><span class="line">584</span><br><span class="line">585</span><br><span class="line">586</span><br><span class="line">587</span><br><span class="line">588</span><br><span class="line">589</span><br><span class="line">590</span><br><span class="line">591</span><br><span class="line">592</span><br><span class="line">593</span><br><span class="line">594</span><br><span class="line">595</span><br><span class="line">596</span><br><span class="line">597</span><br><span class="line">598</span><br><span class="line">599</span><br><span class="line">600</span><br><span class="line">601</span><br><span class="line">602</span><br><span class="line">603</span><br><span class="line">604</span><br><span class="line">605</span><br><span class="line">606</span><br><span class="line">607</span><br><span class="line">608</span><br><span class="line">609</span><br><span class="line">610</span><br><span class="line">611</span><br><span class="line">612</span><br><span class="line">613</span><br><span class="line">614</span><br><span class="line">615</span><br><span class="line">616</span><br><span class="line">617</span><br><span class="line">618</span><br><span class="line">619</span><br><span class="line">620</span><br><span class="line">621</span><br><span class="line">622</span><br><span class="line">623</span><br><span class="line">624</span><br><span class="line">625</span><br><span class="line">626</span><br><span class="line">627</span><br><span class="line">628</span><br><span class="line">629</span><br><span class="line">630</span><br><span class="line">631</span><br><span class="line">632</span><br><span class="line">633</span><br><span class="line">634</span><br><span class="line">635</span><br><span class="line">636</span><br><span class="line">637</span><br><span class="line">638</span><br><span class="line">639</span><br><span class="line">640</span><br><span class="line">641</span><br><span class="line">642</span><br><span class="line">643</span><br><span class="line">644</span><br><span class="line">645</span><br><span class="line">646</span><br><span class="line">647</span><br><span class="line">648</span><br><span class="line">649</span><br><span class="line">650</span><br><span class="line">651</span><br><span class="line">652</span><br><span class="line">653</span><br><span class="line">654</span><br><span class="line">655</span><br><span class="line">656</span><br><span class="line">657</span><br><span class="line">658</span><br><span class="line">659</span><br><span class="line">660</span><br><span class="line">661</span><br><span class="line">662</span><br><span class="line">663</span><br><span class="line">664</span><br><span class="line">665</span><br><span class="line">666</span><br><span class="line">667</span><br><span class="line">668</span><br><span class="line">669</span><br><span class="line">670</span><br><span class="line">671</span><br><span class="line">672</span><br><span class="line">673</span><br><span class="line">674</span><br><span class="line">675</span><br><span class="line">676</span><br><span class="line">677</span><br><span class="line">678</span><br><span class="line">679</span><br><span class="line">680</span><br><span class="line">681</span><br><span class="line">682</span><br><span class="line">683</span><br><span class="line">684</span><br><span class="line">685</span><br><span class="line">686</span><br><span class="line">687</span><br><span class="line">688</span><br><span class="line">689</span><br><span class="line">690</span><br><span class="line">691</span><br><span class="line">692</span><br><span class="line">693</span><br><span class="line">694</span><br><span class="line">695</span><br><span class="line">696</span><br><span class="line">697</span><br><span class="line">698</span><br><span class="line">699</span><br><span class="line">700</span><br><span class="line">701</span><br><span class="line">702</span><br><span class="line">703</span><br><span class="line">704</span><br><span class="line">705</span><br><span class="line">706</span><br><span class="line">707</span><br><span class="line">708</span><br><span class="line">709</span><br><span class="line">710</span><br><span class="line">711</span><br><span class="line">712</span><br><span class="line">713</span><br><span class="line">714</span><br><span class="line">715</span><br><span class="line">716</span><br><span class="line">717</span><br><span class="line">718</span><br><span class="line">719</span><br><span class="line">720</span><br><span class="line">721</span><br><span class="line">722</span><br><span class="line">723</span><br><span class="line">724</span><br><span class="line">725</span><br><span class="line">726</span><br><span class="line">727</span><br><span class="line">728</span><br><span class="line">729</span><br><span class="line">730</span><br><span class="line">731</span><br><span class="line">732</span><br><span class="line">733</span><br><span class="line">734</span><br><span class="line">735</span><br><span class="line">736</span><br><span class="line">737</span><br><span class="line">738</span><br><span class="line">739</span><br><span class="line">740</span><br><span class="line">741</span><br><span class="line">742</span><br><span class="line">743</span><br><span class="line">744</span><br><span class="line">745</span><br><span class="line">746</span><br><span class="line">747</span><br><span class="line">748</span><br><span class="line">749</span><br><span class="line">750</span><br><span class="line">751</span><br><span class="line">752</span><br><span class="line">753</span><br><span class="line">754</span><br><span class="line">755</span><br><span class="line">756</span><br><span class="line">757</span><br><span class="line">758</span><br><span class="line">759</span><br><span class="line">760</span><br><span class="line">761</span><br><span class="line">762</span><br><span class="line">763</span><br><span class="line">764</span><br><span class="line">765</span><br><span class="line">766</span><br><span class="line">767</span><br><span class="line">768</span><br><span class="line">769</span><br><span class="line">770</span><br><span class="line">771</span><br><span class="line">772</span><br><span class="line">773</span><br><span class="line">774</span><br><span class="line">775</span><br><span class="line">776</span><br><span class="line">777</span><br><span class="line">778</span><br><span class="line">779</span><br><span class="line">780</span><br><span class="line">781</span><br><span class="line">782</span><br><span class="line">783</span><br><span class="line">784</span><br><span class="line">785</span><br><span class="line">786</span><br><span class="line">787</span><br><span class="line">788</span><br><span class="line">789</span><br><span class="line">790</span><br><span class="line">791</span><br><span class="line">792</span><br><span class="line">793</span><br><span class="line">794</span><br><span class="line">795</span><br><span class="line">796</span><br><span class="line">797</span><br><span class="line">798</span><br><span class="line">799</span><br><span class="line">800</span><br><span class="line">801</span><br><span class="line">802</span><br><span class="line">803</span><br><span class="line">804</span><br><span class="line">805</span><br><span class="line">806</span><br><span class="line">807</span><br><span class="line">808</span><br><span class="line">809</span><br><span class="line">810</span><br><span class="line">811</span><br><span class="line">812</span><br><span class="line">813</span><br><span class="line">814</span><br><span class="line">815</span><br><span class="line">816</span><br><span class="line">817</span><br><span class="line">818</span><br><span class="line">819</span><br><span class="line">820</span><br><span class="line">821</span><br><span class="line">822</span><br><span class="line">823</span><br><span class="line">824</span><br><span class="line">825</span><br><span class="line">826</span><br><span class="line">827</span><br><span class="line">828</span><br><span class="line">829</span><br><span class="line">830</span><br><span class="line">831</span><br><span class="line">832</span><br><span class="line">833</span><br><span class="line">834</span><br><span class="line">835</span><br><span class="line">836</span><br><span class="line">837</span><br><span class="line">838</span><br><span class="line">839</span><br><span class="line">840</span><br><span class="line">841</span><br><span class="line">842</span><br><span class="line">843</span><br><span class="line">844</span><br><span class="line">845</span><br><span class="line">846</span><br><span class="line">847</span><br><span class="line">848</span><br><span class="line">849</span><br><span class="line">850</span><br><span class="line">851</span><br><span class="line">852</span><br><span class="line">853</span><br><span class="line">854</span><br><span class="line">855</span><br><span class="line">856</span><br><span class="line">857</span><br><span class="line">858</span><br><span class="line">859</span><br><span class="line">860</span><br><span class="line">861</span><br><span class="line">862</span><br><span class="line">863</span><br><span class="line">864</span><br><span class="line">865</span><br><span class="line">866</span><br><span class="line">867</span><br><span class="line">868</span><br><span class="line">869</span><br><span class="line">870</span><br><span class="line">871</span><br><span class="line">872</span><br><span class="line">873</span><br><span class="line">874</span><br><span class="line">875</span><br><span class="line">876</span><br><span class="line">877</span><br><span class="line">878</span><br><span class="line">879</span><br><span class="line">880</span><br><span class="line">881</span><br><span class="line">882</span><br><span class="line">883</span><br><span class="line">884</span><br><span class="line">885</span><br><span class="line">886</span><br><span class="line">887</span><br><span class="line">888</span><br><span class="line">889</span><br><span class="line">890</span><br><span class="line">891</span><br><span class="line">892</span><br><span class="line">893</span><br><span class="line">894</span><br><span class="line">895</span><br><span class="line">896</span><br><span class="line">897</span><br><span class="line">898</span><br><span class="line">899</span><br><span class="line">900</span><br><span class="line">901</span><br><span class="line">902</span><br><span class="line">903</span><br><span class="line">904</span><br><span class="line">905</span><br><span class="line">906</span><br><span class="line">907</span><br><span class="line">908</span><br><span class="line">909</span><br><span class="line">910</span><br><span class="line">911</span><br><span class="line">912</span><br><span class="line">913</span><br><span class="line">914</span><br><span class="line">915</span><br><span class="line">916</span><br><span class="line">917</span><br><span class="line">918</span><br><span class="line">919</span><br><span class="line">920</span><br><span class="line">921</span><br><span class="line">922</span><br><span class="line">923</span><br><span class="line">924</span><br><span class="line">925</span><br><span class="line">926</span><br><span class="line">927</span><br><span class="line">928</span><br><span class="line">929</span><br><span class="line">930</span><br><span class="line">931</span><br><span class="line">932</span><br><span class="line">933</span><br><span class="line">934</span><br><span class="line">935</span><br><span class="line">936</span><br><span class="line">937</span><br><span class="line">938</span><br><span class="line">939</span><br><span class="line">940</span><br><span class="line">941</span><br><span class="line">942</span><br><span class="line">943</span><br><span class="line">944</span><br><span class="line">945</span><br><span class="line">946</span><br><span class="line">947</span><br><span class="line">948</span><br><span class="line">949</span><br><span class="line">950</span><br><span class="line">951</span><br><span class="line">952</span><br><span class="line">953</span><br><span class="line">954</span><br><span class="line">955</span><br><span class="line">956</span><br><span class="line">957</span><br><span class="line">958</span><br><span class="line">959</span><br><span class="line">960</span><br><span class="line">961</span><br><span class="line">962</span><br><span class="line">963</span><br><span class="line">964</span><br><span class="line">965</span><br><span class="line">966</span><br><span class="line">967</span><br><span class="line">968</span><br><span class="line">969</span><br><span class="line">970</span><br><span class="line">971</span><br><span class="line">972</span><br><span class="line">973</span><br><span class="line">974</span><br><span class="line">975</span><br><span class="line">976</span><br><span class="line">977</span><br><span class="line">978</span><br><span class="line">979</span><br><span class="line">980</span><br><span class="line">981</span><br><span class="line">982</span><br><span class="line">983</span><br><span class="line">984</span><br><span class="line">985</span><br><span class="line">986</span><br><span class="line">987</span><br><span class="line">988</span><br><span class="line">989</span><br><span class="line">990</span><br><span class="line">991</span><br><span class="line">992</span><br><span class="line">993</span><br><span class="line">994</span><br><span class="line">995</span><br><span class="line">996</span><br><span class="line">997</span><br><span class="line">998</span><br><span class="line">999</span><br><span class="line">1000</span><br><span class="line">1001</span><br><span class="line">1002</span><br><span class="line">1003</span><br><span class="line">1004</span><br><span class="line">1005</span><br><span class="line">1006</span><br><span class="line">1007</span><br><span class="line">1008</span><br><span class="line">1009</span><br><span class="line">1010</span><br><span class="line">1011</span><br><span class="line">1012</span><br><span class="line">1013</span><br><span class="line">1014</span><br><span class="line">1015</span><br><span class="line">1016</span><br><span class="line">1017</span><br><span class="line">1018</span><br><span class="line">1019</span><br><span class="line">1020</span><br><span class="line">1021</span><br><span class="line">1022</span><br><span class="line">1023</span><br><span class="line">1024</span><br><span class="line">1025</span><br><span class="line">1026</span><br><span class="line">1027</span><br><span class="line">1028</span><br><span class="line">1029</span><br><span class="line">1030</span><br><span class="line">1031</span><br><span class="line">1032</span><br><span class="line">1033</span><br><span class="line">1034</span><br><span class="line">1035</span><br><span class="line">1036</span><br><span class="line">1037</span><br><span class="line">1038</span><br><span class="line">1039</span><br><span class="line">1040</span><br><span class="line">1041</span><br><span class="line">1042</span><br><span class="line">1043</span><br><span class="line">1044</span><br><span class="line">1045</span><br><span class="line">1046</span><br><span class="line">1047</span><br><span class="line">1048</span><br><span class="line">1049</span><br><span class="line">1050</span><br><span class="line">1051</span><br><span class="line">1052</span><br><span class="line">1053</span><br><span class="line">1054</span><br><span class="line">1055</span><br><span class="line">1056</span><br><span class="line">1057</span><br><span class="line">1058</span><br><span class="line">1059</span><br><span class="line">1060</span><br><span class="line">1061</span><br><span class="line">1062</span><br><span class="line">1063</span><br><span class="line">1064</span><br><span class="line">1065</span><br><span class="line">1066</span><br><span class="line">1067</span><br><span class="line">1068</span><br><span class="line">1069</span><br><span class="line">1070</span><br><span class="line">1071</span><br><span class="line">1072</span><br><span class="line">1073</span><br><span class="line">1074</span><br><span class="line">1075</span><br><span class="line">1076</span><br><span class="line">1077</span><br><span class="line">1078</span><br><span class="line">1079</span><br><span class="line">1080</span><br><span class="line">1081</span><br><span class="line">1082</span><br><span class="line">1083</span><br><span class="line">1084</span><br><span class="line">1085</span><br><span class="line">1086</span><br><span class="line">1087</span><br><span class="line">1088</span><br><span class="line">1089</span><br><span class="line">1090</span><br><span class="line">1091</span><br><span class="line">1092</span><br><span class="line">1093</span><br><span class="line">1094</span><br><span class="line">1095</span><br><span class="line">1096</span><br><span class="line">1097</span><br><span class="line">1098</span><br><span class="line">1099</span><br><span class="line">1100</span><br><span class="line">1101</span><br><span class="line">1102</span><br><span class="line">1103</span><br><span class="line">1104</span><br><span class="line">1105</span><br><span class="line">1106</span><br><span class="line">1107</span><br><span class="line">1108</span><br><span class="line">1109</span><br><span class="line">1110</span><br><span class="line">1111</span><br><span class="line">1112</span><br><span class="line">1113</span><br><span class="line">1114</span><br><span class="line">1115</span><br><span class="line">1116</span><br><span class="line">1117</span><br><span class="line">1118</span><br><span class="line">1119</span><br><span class="line">1120</span><br><span class="line">1121</span><br><span class="line">1122</span><br><span class="line">1123</span><br><span class="line">1124</span><br><span class="line">1125</span><br><span class="line">1126</span><br><span class="line">1127</span><br><span class="line">1128</span><br><span class="line">1129</span><br><span class="line">1130</span><br><span class="line">1131</span><br><span class="line">1132</span><br><span class="line">1133</span><br><span class="line">1134</span><br><span class="line">1135</span><br><span class="line">1136</span><br><span class="line">1137</span><br><span class="line">1138</span><br><span class="line">1139</span><br><span class="line">1140</span><br><span class="line">1141</span><br><span class="line">1142</span><br><span class="line">1143</span><br><span class="line">1144</span><br><span class="line">1145</span><br><span class="line">1146</span><br><span class="line">1147</span><br><span class="line">1148</span><br><span class="line">1149</span><br><span class="line">1150</span><br><span class="line">1151</span><br><span class="line">1152</span><br><span class="line">1153</span><br><span class="line">1154</span><br><span class="line">1155</span><br><span class="line">1156</span><br><span class="line">1157</span><br><span class="line">1158</span><br><span class="line">1159</span><br><span class="line">1160</span><br><span class="line">1161</span><br><span class="line">1162</span><br><span class="line">1163</span><br><span class="line">1164</span><br><span class="line">1165</span><br><span class="line">1166</span><br><span class="line">1167</span><br><span class="line">1168</span><br><span class="line">1169</span><br><span class="line">1170</span><br><span class="line">1171</span><br><span class="line">1172</span><br><span class="line">1173</span><br><span class="line">1174</span><br><span class="line">1175</span><br><span class="line">1176</span><br><span class="line">1177</span><br><span class="line">1178</span><br><span class="line">1179</span><br><span class="line">1180</span><br><span class="line">1181</span><br><span class="line">1182</span><br><span class="line">1183</span><br><span class="line">1184</span><br><span class="line">1185</span><br><span class="line">1186</span><br><span class="line">1187</span><br><span class="line">1188</span><br><span class="line">1189</span><br><span class="line">1190</span><br><span class="line">1191</span><br><span class="line">1192</span><br><span class="line">1193</span><br><span class="line">1194</span><br><span class="line">1195</span><br><span class="line">1196</span><br><span class="line">1197</span><br><span class="line">1198</span><br><span class="line">1199</span><br><span class="line">1200</span><br><span class="line">1201</span><br><span class="line">1202</span><br><span class="line">1203</span><br><span class="line">1204</span><br><span class="line">1205</span><br><span class="line">1206</span><br><span class="line">1207</span><br><span class="line">1208</span><br><span class="line">1209</span><br><span class="line">1210</span><br><span class="line">1211</span><br><span class="line">1212</span><br><span class="line">1213</span><br><span class="line">1214</span><br><span class="line">1215</span><br><span class="line">1216</span><br><span class="line">1217</span><br><span class="line">1218</span><br><span class="line">1219</span><br><span class="line">1220</span><br><span class="line">1221</span><br><span class="line">1222</span><br><span class="line">1223</span><br><span class="line">1224</span><br><span class="line">1225</span><br><span class="line">1226</span><br><span class="line">1227</span><br><span class="line">1228</span><br><span class="line">1229</span><br><span class="line">1230</span><br><span class="line">1231</span><br><span class="line">1232</span><br><span class="line">1233</span><br><span class="line">1234</span><br><span class="line">1235</span><br><span class="line">1236</span><br><span class="line">1237</span><br><span class="line">1238</span><br><span class="line">1239</span><br><span class="line">1240</span><br><span class="line">1241</span><br><span class="line">1242</span><br><span class="line">1243</span><br><span class="line">1244</span><br><span class="line">1245</span><br><span class="line">1246</span><br><span class="line">1247</span><br><span class="line">1248</span><br><span class="line">1249</span><br><span class="line">1250</span><br><span class="line">1251</span><br><span class="line">1252</span><br><span class="line">1253</span><br><span class="line">1254</span><br><span class="line">1255</span><br><span class="line">1256</span><br><span class="line">1257</span><br><span class="line">1258</span><br><span class="line">1259</span><br><span class="line">1260</span><br><span class="line">1261</span><br><span class="line">1262</span><br><span class="line">1263</span><br><span class="line">1264</span><br><span class="line">1265</span><br><span class="line">1266</span><br><span class="line">1267</span><br><span class="line">1268</span><br><span class="line">1269</span><br><span class="line">1270</span><br><span class="line">1271</span><br><span class="line">1272</span><br><span class="line">1273</span><br><span class="line">1274</span><br><span class="line">1275</span><br><span class="line">1276</span><br><span class="line">1277</span><br><span class="line">1278</span><br><span class="line">1279</span><br><span class="line">1280</span><br><span class="line">1281</span><br><span class="line">1282</span><br><span class="line">1283</span><br><span class="line">1284</span><br><span class="line">1285</span><br><span class="line">1286</span><br><span class="line">1287</span><br><span class="line">1288</span><br><span class="line">1289</span><br><span class="line">1290</span><br><span class="line">1291</span><br><span class="line">1292</span><br><span class="line">1293</span><br><span class="line">1294</span><br><span class="line">1295</span><br><span class="line">1296</span><br><span class="line">1297</span><br><span class="line">1298</span><br><span class="line">1299</span><br><span class="line">1300</span><br><span class="line">1301</span><br><span class="line">1302</span><br><span class="line">1303</span><br><span class="line">1304</span><br><span class="line">1305</span><br><span class="line">1306</span><br><span class="line">1307</span><br><span class="line">1308</span><br><span class="line">1309</span><br><span class="line">1310</span><br><span class="line">1311</span><br><span class="line">1312</span><br><span class="line">1313</span><br><span class="line">1314</span><br><span class="line">1315</span><br><span class="line">1316</span><br><span class="line">1317</span><br><span class="line">1318</span><br><span class="line">1319</span><br><span class="line">1320</span><br><span class="line">1321</span><br><span class="line">1322</span><br><span class="line">1323</span><br><span class="line">1324</span><br><span class="line">1325</span><br><span class="line">1326</span><br><span class="line">1327</span><br><span class="line">1328</span><br><span class="line">1329</span><br><span class="line">1330</span><br><span class="line">1331</span><br><span class="line">1332</span><br><span class="line">1333</span><br><span class="line">1334</span><br><span class="line">1335</span><br><span class="line">1336</span><br><span class="line">1337</span><br><span class="line">1338</span><br><span class="line">1339</span><br><span class="line">1340</span><br><span class="line">1341</span><br><span class="line">1342</span><br><span class="line">1343</span><br><span class="line">1344</span><br><span class="line">1345</span><br><span class="line">1346</span><br><span class="line">1347</span><br><span class="line">1348</span><br><span class="line">1349</span><br><span class="line">1350</span><br><span class="line">1351</span><br><span class="line">1352</span><br><span class="line">1353</span><br><span class="line">1354</span><br><span class="line">1355</span><br><span class="line">1356</span><br><span class="line">1357</span><br><span class="line">1358</span><br><span class="line">1359</span><br><span class="line">1360</span><br><span class="line">1361</span><br><span class="line">1362</span><br><span class="line">1363</span><br><span class="line">1364</span><br><span class="line">1365</span><br><span class="line">1366</span><br><span class="line">1367</span><br><span class="line">1368</span><br><span class="line">1369</span><br><span class="line">1370</span><br><span class="line">1371</span><br><span class="line">1372</span><br><span class="line">1373</span><br><span class="line">1374</span><br><span class="line">1375</span><br><span class="line">1376</span><br><span class="line">1377</span><br><span class="line">1378</span><br><span class="line">1379</span><br><span class="line">1380</span><br><span class="line">1381</span><br><span class="line">1382</span><br><span class="line">1383</span><br><span class="line">1384</span><br><span class="line">1385</span><br><span class="line">1386</span><br><span class="line">1387</span><br><span class="line">1388</span><br><span class="line">1389</span><br><span class="line">1390</span><br><span class="line">1391</span><br><span class="line">1392</span><br><span class="line">1393</span><br><span class="line">1394</span><br><span class="line">1395</span><br><span class="line">1396</span><br><span class="line">1397</span><br><span class="line">1398</span><br><span class="line">1399</span><br><span class="line">1400</span><br><span class="line">1401</span><br><span class="line">1402</span><br><span class="line">1403</span><br><span class="line">1404</span><br><span class="line">1405</span><br><span class="line">1406</span><br><span class="line">1407</span><br><span class="line">1408</span><br><span class="line">1409</span><br><span class="line">1410</span><br><span class="line">1411</span><br><span class="line">1412</span><br><span class="line">1413</span><br><span class="line">1414</span><br><span class="line">1415</span><br><span class="line">1416</span><br><span class="line">1417</span><br><span class="line">1418</span><br><span class="line">1419</span><br><span class="line">1420</span><br><span class="line">1421</span><br><span class="line">1422</span><br><span class="line">1423</span><br><span class="line">1424</span><br><span class="line">1425</span><br><span class="line">1426</span><br><span class="line">1427</span><br><span class="line">1428</span><br><span class="line">1429</span><br><span class="line">1430</span><br><span class="line">1431</span><br><span class="line">1432</span><br><span class="line">1433</span><br><span class="line">1434</span><br><span class="line">1435</span><br><span class="line">1436</span><br><span class="line">1437</span><br><span class="line">1438</span><br><span class="line">1439</span><br><span class="line">1440</span><br><span class="line">1441</span><br><span class="line">1442</span><br><span class="line">1443</span><br><span class="line">1444</span><br><span class="line">1445</span><br><span class="line">1446</span><br><span class="line">1447</span><br><span class="line">1448</span><br><span class="line">1449</span><br><span class="line">1450</span><br><span class="line">1451</span><br><span class="line">1452</span><br><span class="line">1453</span><br><span class="line">1454</span><br><span class="line">1455</span><br><span class="line">1456</span><br><span class="line">1457</span><br><span class="line">1458</span><br><span class="line">1459</span><br><span class="line">1460</span><br><span class="line">1461</span><br><span class="line">1462</span><br><span class="line">1463</span><br><span class="line">1464</span><br><span class="line">1465</span><br><span class="line">1466</span><br><span class="line">1467</span><br><span class="line">1468</span><br><span class="line">1469</span><br><span class="line">1470</span><br><span class="line">1471</span><br><span class="line">1472</span><br><span class="line">1473</span><br><span class="line">1474</span><br><span class="line">1475</span><br><span class="line">1476</span><br><span class="line">1477</span><br><span class="line">1478</span><br><span class="line">1479</span><br><span class="line">1480</span><br><span class="line">1481</span><br><span class="line">1482</span><br><span class="line">1483</span><br><span class="line">1484</span><br><span class="line">1485</span><br><span class="line">1486</span><br><span class="line">1487</span><br><span class="line">1488</span><br><span class="line">1489</span><br><span class="line">1490</span><br><span class="line">1491</span><br><span class="line">1492</span><br><span class="line">1493</span><br><span class="line">1494</span><br><span class="line">1495</span><br><span class="line">1496</span><br><span class="line">1497</span><br><span class="line">1498</span><br><span class="line">1499</span><br><span class="line">1500</span><br><span class="line">1501</span><br><span class="line">1502</span><br><span class="line">1503</span><br><span class="line">1504</span><br><span class="line">1505</span><br><span class="line">1506</span><br><span class="line">1507</span><br><span class="line">1508</span><br><span class="line">1509</span><br><span class="line">1510</span><br><span class="line">1511</span><br><span class="line">1512</span><br><span class="line">1513</span><br><span class="line">1514</span><br><span class="line">1515</span><br><span class="line">1516</span><br><span class="line">1517</span><br><span class="line">1518</span><br><span class="line">1519</span><br><span class="line">1520</span><br><span class="line">1521</span><br><span class="line">1522</span><br><span class="line">1523</span><br><span class="line">1524</span><br><span class="line">1525</span><br><span class="line">1526</span><br><span class="line">1527</span><br><span class="line">1528</span><br><span class="line">1529</span><br><span class="line">1530</span><br><span class="line">1531</span><br><span class="line">1532</span><br><span class="line">1533</span><br><span class="line">1534</span><br><span class="line">1535</span><br><span class="line">1536</span><br><span class="line">1537</span><br><span class="line">1538</span><br><span class="line">1539</span><br><span class="line">1540</span><br><span class="line">1541</span><br><span class="line">1542</span><br><span class="line">1543</span><br><span class="line">1544</span><br><span class="line">1545</span><br><span class="line">1546</span><br><span class="line">1547</span><br><span class="line">1548</span><br><span class="line">1549</span><br><span class="line">1550</span><br><span class="line">1551</span><br><span class="line">1552</span><br><span class="line">1553</span><br><span class="line">1554</span><br><span class="line">1555</span><br><span class="line">1556</span><br><span class="line">1557</span><br><span class="line">1558</span><br><span class="line">1559</span><br><span class="line">1560</span><br><span class="line">1561</span><br><span class="line">1562</span><br><span class="line">1563</span><br><span class="line">1564</span><br><span class="line">1565</span><br><span class="line">1566</span><br><span class="line">1567</span><br><span class="line">1568</span><br><span class="line">1569</span><br><span class="line">1570</span><br><span class="line">1571</span><br><span class="line">1572</span><br><span class="line">1573</span><br><span class="line">1574</span><br><span class="line">1575</span><br><span class="line">1576</span><br><span class="line">1577</span><br><span class="line">1578</span><br><span class="line">1579</span><br><span class="line">1580</span><br><span class="line">1581</span><br><span class="line">1582</span><br><span class="line">1583</span><br><span class="line">1584</span><br><span class="line">1585</span><br><span class="line">1586</span><br><span class="line">1587</span><br><span class="line">1588</span><br><span class="line">1589</span><br><span class="line">1590</span><br><span class="line">1591</span><br><span class="line">1592</span><br><span class="line">1593</span><br><span class="line">1594</span><br><span class="line">1595</span><br><span class="line">1596</span><br><span class="line">1597</span><br><span class="line">1598</span><br><span class="line">1599</span><br><span class="line">1600</span><br><span class="line">1601</span><br><span class="line">1602</span><br><span class="line">1603</span><br><span class="line">1604</span><br><span class="line">1605</span><br><span class="line">1606</span><br><span class="line">1607</span><br><span class="line">1608</span><br><span class="line">1609</span><br><span class="line">1610</span><br><span class="line">1611</span><br><span class="line">1612</span><br><span class="line">1613</span><br><span class="line">1614</span><br><span class="line">1615</span><br><span class="line">1616</span><br><span class="line">1617</span><br><span class="line">1618</span><br><span class="line">1619</span><br><span class="line">1620</span><br><span class="line">1621</span><br><span class="line">1622</span><br><span class="line">1623</span><br><span class="line">1624</span><br><span class="line">1625</span><br><span class="line">1626</span><br><span class="line">1627</span><br><span class="line">1628</span><br><span class="line">1629</span><br><span class="line">1630</span><br><span class="line">1631</span><br><span class="line">1632</span><br><span class="line">1633</span><br><span class="line">1634</span><br><span class="line">1635</span><br><span class="line">1636</span><br><span class="line">1637</span><br><span class="line">1638</span><br><span class="line">1639</span><br><span class="line">1640</span><br><span class="line">1641</span><br><span class="line">1642</span><br><span class="line">1643</span><br><span class="line">1644</span><br><span class="line">1645</span><br><span class="line">1646</span><br><span class="line">1647</span><br><span class="line">1648</span><br><span class="line">1649</span><br><span class="line">1650</span><br><span class="line">1651</span><br><span class="line">1652</span><br><span class="line">1653</span><br><span class="line">1654</span><br><span class="line">1655</span><br><span class="line">1656</span><br><span class="line">1657</span><br><span class="line">1658</span><br><span class="line">1659</span><br><span class="line">1660</span><br><span class="line">1661</span><br><span class="line">1662</span><br><span class="line">1663</span><br><span class="line">1664</span><br><span class="line">1665</span><br><span class="line">1666</span><br><span class="line">1667</span><br><span class="line">1668</span><br><span class="line">1669</span><br><span class="line">1670</span><br><span class="line">1671</span><br><span class="line">1672</span><br><span class="line">1673</span><br><span class="line">1674</span><br><span class="line">1675</span><br><span class="line">1676</span><br><span class="line">1677</span><br><span class="line">1678</span><br><span class="line">1679</span><br><span class="line">1680</span><br><span class="line">1681</span><br><span class="line">1682</span><br><span class="line">1683</span><br><span class="line">1684</span><br><span class="line">1685</span><br><span class="line">1686</span><br><span class="line">1687</span><br><span class="line">1688</span><br><span class="line">1689</span><br><span class="line">1690</span><br><span class="line">1691</span><br><span class="line">1692</span><br><span class="line">1693</span><br><span class="line">1694</span><br><span class="line">1695</span><br><span class="line">1696</span><br><span class="line">1697</span><br><span class="line">1698</span><br><span class="line">1699</span><br><span class="line">1700</span><br><span class="line">1701</span><br><span class="line">1702</span><br><span class="line">1703</span><br><span class="line">1704</span><br><span class="line">1705</span><br><span class="line">1706</span><br><span class="line">1707</span><br><span class="line">1708</span><br><span class="line">1709</span><br><span class="line">1710</span><br><span class="line">1711</span><br><span class="line">1712</span><br><span class="line">1713</span><br><span class="line">1714</span><br><span class="line">1715</span><br><span class="line">1716</span><br><span class="line">1717</span><br><span class="line">1718</span><br><span class="line">1719</span><br><span class="line">1720</span><br><span class="line">1721</span><br><span class="line">1722</span><br><span class="line">1723</span><br><span class="line">1724</span><br><span class="line">1725</span><br><span class="line">1726</span><br><span class="line">1727</span><br><span class="line">1728</span><br><span class="line">1729</span><br><span class="line">1730</span><br><span class="line">1731</span><br><span class="line">1732</span><br><span class="line">1733</span><br><span class="line">1734</span><br><span class="line">1735</span><br><span class="line">1736</span><br><span class="line">1737</span><br><span class="line">1738</span><br><span class="line">1739</span><br><span class="line">1740</span><br><span class="line">1741</span><br><span class="line">1742</span><br><span class="line">1743</span><br><span class="line">1744</span><br><span class="line">1745</span><br><span class="line">1746</span><br><span class="line">1747</span><br><span class="line">1748</span><br><span class="line">1749</span><br><span class="line">1750</span><br><span class="line">1751</span><br><span class="line">1752</span><br><span class="line">1753</span><br><span class="line">1754</span><br><span class="line">1755</span><br><span class="line">1756</span><br><span class="line">1757</span><br><span class="line">1758</span><br><span class="line">1759</span><br><span class="line">1760</span><br><span class="line">1761</span><br><span class="line">1762</span><br><span class="line">1763</span><br><span class="line">1764</span><br><span class="line">1765</span><br><span class="line">1766</span><br><span class="line">1767</span><br><span class="line">1768</span><br><span class="line">1769</span><br><span class="line">1770</span><br><span class="line">1771</span><br><span class="line">1772</span><br><span class="line">1773</span><br><span class="line">1774</span><br><span class="line">1775</span><br><span class="line">1776</span><br><span class="line">1777</span><br><span class="line">1778</span><br><span class="line">1779</span><br><span class="line">1780</span><br><span class="line">1781</span><br><span class="line">1782</span><br><span class="line">1783</span><br><span class="line">1784</span><br><span class="line">1785</span><br><span class="line">1786</span><br><span class="line">1787</span><br><span class="line">1788</span><br><span class="line">1789</span><br><span class="line">1790</span><br><span class="line">1791</span><br><span class="line">1792</span><br><span class="line">1793</span><br><span class="line">1794</span><br><span class="line">1795</span><br><span class="line">1796</span><br><span class="line">1797</span><br><span class="line">1798</span><br><span class="line">1799</span><br><span class="line">1800</span><br><span class="line">1801</span><br><span class="line">1802</span><br><span class="line">1803</span><br><span class="line">1804</span><br><span class="line">1805</span><br><span class="line">1806</span><br><span class="line">1807</span><br><span class="line">1808</span><br><span class="line">1809</span><br><span class="line">1810</span><br><span class="line">1811</span><br><span class="line">1812</span><br><span class="line">1813</span><br><span class="line">1814</span><br><span class="line">1815</span><br><span class="line">1816</span><br><span class="line">1817</span><br><span class="line">1818</span><br><span class="line">1819</span><br><span class="line">1820</span><br><span class="line">1821</span><br><span class="line">1822</span><br><span class="line">1823</span><br><span class="line">1824</span><br><span class="line">1825</span><br><span class="line">1826</span><br><span class="line">1827</span><br><span class="line">1828</span><br><span class="line">1829</span><br><span class="line">1830</span><br><span class="line">1831</span><br><span class="line">1832</span><br><span class="line">1833</span><br><span class="line">1834</span><br><span class="line">1835</span><br><span class="line">1836</span><br><span class="line">1837</span><br><span class="line">1838</span><br><span class="line">1839</span><br><span class="line">1840</span><br><span class="line">1841</span><br><span class="line">1842</span><br><span class="line">1843</span><br><span class="line">1844</span><br><span class="line">1845</span><br><span class="line">1846</span><br><span class="line">1847</span><br><span class="line">1848</span><br><span class="line">1849</span><br><span class="line">1850</span><br><span class="line">1851</span><br><span class="line">1852</span><br><span class="line">1853</span><br><span class="line">1854</span><br><span class="line">1855</span><br><span class="line">1856</span><br><span class="line">1857</span><br><span class="line">1858</span><br><span class="line">1859</span><br><span class="line">1860</span><br><span class="line">1861</span><br><span class="line">1862</span><br><span class="line">1863</span><br><span class="line">1864</span><br><span class="line">1865</span><br><span class="line">1866</span><br><span class="line">1867</span><br><span class="line">1868</span><br><span class="line">1869</span><br><span class="line">1870</span><br><span class="line">1871</span><br><span class="line">1872</span><br><span class="line">1873</span><br><span class="line">1874</span><br><span class="line">1875</span><br><span class="line">1876</span><br><span class="line">1877</span><br><span class="line">1878</span><br><span class="line">1879</span><br><span class="line">1880</span><br><span class="line">1881</span><br><span class="line">1882</span><br><span class="line">1883</span><br><span class="line">1884</span><br><span class="line">1885</span><br><span class="line">1886</span><br><span class="line">1887</span><br><span class="line">1888</span><br><span class="line">1889</span><br><span class="line">1890</span><br><span class="line">1891</span><br><span class="line">1892</span><br><span class="line">1893</span><br><span class="line">1894</span><br><span class="line">1895</span><br><span class="line">1896</span><br><span class="line">1897</span><br><span class="line">1898</span><br><span class="line">1899</span><br><span class="line">1900</span><br><span class="line">1901</span><br><span class="line">1902</span><br><span class="line">1903</span><br><span class="line">1904</span><br><span class="line">1905</span><br><span class="line">1906</span><br><span class="line">1907</span><br><span class="line">1908</span><br><span class="line">1909</span><br><span class="line">1910</span><br><span class="line">1911</span><br><span class="line">1912</span><br><span class="line">1913</span><br><span class="line">1914</span><br><span class="line">1915</span><br><span class="line">1916</span><br><span class="line">1917</span><br><span class="line">1918</span><br><span class="line">1919</span><br><span class="line">1920</span><br><span class="line">1921</span><br><span class="line">1922</span><br><span class="line">1923</span><br><span class="line">1924</span><br><span class="line">1925</span><br><span class="line">1926</span><br><span class="line">1927</span><br><span class="line">1928</span><br><span class="line">1929</span><br><span class="line">1930</span><br><span class="line">1931</span><br><span class="line">1932</span><br><span class="line">1933</span><br><span class="line">1934</span><br><span class="line">1935</span><br><span class="line">1936</span><br><span class="line">1937</span><br><span class="line">1938</span><br><span class="line">1939</span><br><span class="line">1940</span><br><span class="line">1941</span><br><span class="line">1942</span><br><span class="line">1943</span><br><span class="line">1944</span><br><span class="line">1945</span><br><span class="line">1946</span><br><span class="line">1947</span><br><span class="line">1948</span><br><span class="line">1949</span><br><span class="line">1950</span><br><span class="line">1951</span><br><span class="line">1952</span><br><span class="line">1953</span><br><span class="line">1954</span><br><span class="line">1955</span><br><span class="line">1956</span><br><span class="line">1957</span><br><span class="line">1958</span><br><span class="line">1959</span><br><span class="line">1960</span><br><span class="line">1961</span><br><span class="line">1962</span><br><span class="line">1963</span><br><span class="line">1964</span><br><span class="line">1965</span><br><span class="line">1966</span><br><span class="line">1967</span><br><span class="line">1968</span><br><span class="line">1969</span><br><span class="line">1970</span><br><span class="line">1971</span><br><span class="line">1972</span><br><span class="line">1973</span><br><span class="line">1974</span><br><span class="line">1975</span><br><span class="line">1976</span><br><span class="line">1977</span><br><span class="line">1978</span><br><span class="line">1979</span><br><span class="line">1980</span><br><span class="line">1981</span><br><span class="line">1982</span><br><span class="line">1983</span><br><span class="line">1984</span><br><span class="line">1985</span><br><span class="line">1986</span><br><span class="line">1987</span><br><span class="line">1988</span><br><span class="line">1989</span><br><span class="line">1990</span><br><span class="line">1991</span><br><span class="line">1992</span><br><span class="line">1993</span><br><span class="line">1994</span><br><span class="line">1995</span><br><span class="line">1996</span><br><span class="line">1997</span><br><span class="line">1998</span><br><span class="line">1999</span><br><span class="line">2000</span><br><span class="line">2001</span><br><span class="line">2002</span><br><span class="line">2003</span><br><span class="line">2004</span><br><span class="line">2005</span><br><span class="line">2006</span><br><span class="line">2007</span><br><span class="line">2008</span><br><span class="line">2009</span><br><span class="line">2010</span><br><span class="line">2011</span><br><span class="line">2012</span><br><span class="line">2013</span><br><span class="line">2014</span><br><span class="line">2015</span><br><span class="line">2016</span><br><span class="line">2017</span><br><span class="line">2018</span><br><span class="line">2019</span><br><span class="line">2020</span><br><span class="line">2021</span><br><span class="line">2022</span><br><span class="line">2023</span><br><span class="line">2024</span><br><span class="line">2025</span><br><span class="line">2026</span><br><span class="line">2027</span><br><span class="line">2028</span><br><span class="line">2029</span><br><span class="line">2030</span><br><span class="line">2031</span><br><span class="line">2032</span><br><span class="line">2033</span><br><span class="line">2034</span><br><span class="line">2035</span><br><span class="line">2036</span><br><span class="line">2037</span><br><span class="line">2038</span><br><span class="line">2039</span><br><span class="line">2040</span><br><span class="line">2041</span><br><span class="line">2042</span><br><span class="line">2043</span><br><span class="line">2044</span><br><span class="line">2045</span><br><span class="line">2046</span><br><span class="line">2047</span><br><span class="line">2048</span><br><span class="line">2049</span><br><span class="line">2050</span><br><span class="line">2051</span><br><span class="line">2052</span><br><span class="line">2053</span><br><span class="line">2054</span><br><span class="line">2055</span><br><span class="line">2056</span><br><span class="line">2057</span><br><span class="line">2058</span><br><span class="line">2059</span><br><span class="line">2060</span><br><span class="line">2061</span><br><span class="line">2062</span><br><span class="line">2063</span><br><span class="line">2064</span><br><span class="line">2065</span><br><span class="line">2066</span><br><span class="line">2067</span><br><span class="line">2068</span><br><span class="line">2069</span><br><span class="line">2070</span><br><span class="line">2071</span><br><span class="line">2072</span><br><span class="line">2073</span><br><span class="line">2074</span><br><span class="line">2075</span><br><span class="line">2076</span><br><span class="line">2077</span><br><span class="line">2078</span><br><span class="line">2079</span><br><span class="line">2080</span><br><span class="line">2081</span><br><span class="line">2082</span><br><span class="line">2083</span><br><span class="line">2084</span><br><span class="line">2085</span><br><span class="line">2086</span><br><span class="line">2087</span><br><span class="line">2088</span><br><span class="line">2089</span><br><span class="line">2090</span><br><span class="line">2091</span><br><span class="line">2092</span><br><span class="line">2093</span><br><span class="line">2094</span><br><span class="line">2095</span><br><span class="line">2096</span><br><span class="line">2097</span><br><span class="line">2098</span><br><span class="line">2099</span><br><span class="line">2100</span><br><span class="line">2101</span><br><span class="line">2102</span><br><span class="line">2103</span><br><span class="line">2104</span><br><span class="line">2105</span><br><span class="line">2106</span><br><span class="line">2107</span><br><span class="line">2108</span><br><span class="line">2109</span><br><span class="line">2110</span><br><span class="line">2111</span><br><span class="line">2112</span><br><span class="line">2113</span><br><span class="line">2114</span><br><span class="line">2115</span><br><span class="line">2116</span><br><span class="line">2117</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;zephyr.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;errno.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;atomic.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;misc/util.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;misc/byteorder.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;net/buf.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bluetooth.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;conn.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;api/mesh.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;uuid.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;genie_mesh_flash.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_PROV)</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;common/log.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> MESH_DEBUG_PROV</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ALI_PROV_TAG <span class="string">&quot;\t[ALI_PROV]&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_D(f, ...) printf(<span class="string">&quot;%d &quot;</span>ALI_PROV_TAG<span class="string">&quot;[D] %s &quot;</span>f<span class="string">&quot;\n&quot;</span>, (u32_t)aos_now_ms(), __func__, ##__VA_ARGS__)</span></span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_D(f, ...)</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;ecc.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;port/mesh_hal_ble.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;mesh_crypto.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;adv.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;mesh.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;net.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;access.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;foundation.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;proxy.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;mesh/main.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;bt_mesh_custom_log.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span>  CONFIG_BT_MESH_ROLE_PROVISIONER</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;provisioner_prov.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;tri_tuple_default.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 4 transmissions, 20ms interval */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_XMIT_COUNT        0</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_XMIT_INT          20</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> AUTH_METHOD_NO_OOB     0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> AUTH_METHOD_STATIC     0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> AUTH_METHOD_OUTPUT     0x02</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> AUTH_METHOD_INPUT      0x03</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> OUTPUT_OOB_BLINK       0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> OUTPUT_OOB_BEEP        0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> OUTPUT_OOB_VIBRATE     0x02</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> OUTPUT_OOB_NUMBER      0x03</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> OUTPUT_OOB_STRING      0x04</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INPUT_OOB_PUSH         0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INPUT_OOB_TWIST        0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INPUT_OOB_NUMBER       0x02</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> INPUT_OOB_STRING       0x03</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_NONE          0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_NVAL_PDU      0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_NVAL_FMT      0x02</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_UNEXP_PDU     0x03</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_CFM_FAILED    0x04</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_RESOURCES     0x05</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_DECRYPT       0x06</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_UNEXP_ERR     0x07</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ERR_ADDR          0x08</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_INVITE            0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_CAPABILITIES      0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_START             0x02</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_PUB_KEY           0x03</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_INPUT_COMPLETE    0x04</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_CONFIRM           0x05</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_RANDOM            0x06</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_DATA              0x07</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_FAILED            0x09</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_COMPLETE          0x08</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_ALG_P256          0x00</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPCF(gpc)              (gpc &amp; 0x03)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPC_START(last_seg)    (((last_seg) &lt;&lt; 2) | 0x00)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPC_ACK                0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPC_CONT(seg_id)       (((seg_id) &lt;&lt; 2) | 0x02)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPC_CTL(op)            (((op) &lt;&lt; 2) | 0x03)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> START_PAYLOAD_MAX      20</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONT_PAYLOAD_MAX       23</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> START_LAST_SEG(gpc)    (gpc &gt;&gt; 2)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONT_SEG_INDEX(gpc)    (gpc &gt;&gt; 2)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BEARER_CTL(gpc)        (gpc &gt;&gt; 2)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LINK_OPEN              0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LINK_ACK               0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> LINK_CLOSE             0x02</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CLOSE_REASON_SUCCESS   0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CLOSE_REASON_TIMEOUT   0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CLOSE_REASON_FAILED    0x02</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_BUF(len) NET_BUF_SIMPLE(PROV_BUF_HEADROOM + len)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> XACT_SEG_DATA(_seg) (&amp;link.rx.buf-&gt;data[20 + ((_seg - 1) * 23)])</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> XACT_SEG_RECV(_seg) (link.rx.seg &amp;= ~(1 &lt;&lt; (_seg)))</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> XACT_NVAL              0xff</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">enum</span> &#123;</span></span><br><span class="line">    REMOTE_PUB_KEY,        <span class="comment">/* Remote key has been received */</span></span><br><span class="line">    LOCAL_PUB_KEY,         <span class="comment">/* Local public key is available */</span></span><br><span class="line">    LINK_ACTIVE,           <span class="comment">/* Link has been opened */</span></span><br><span class="line">    WAIT_GEN_DHKEY,        <span class="comment">/* Waiting for remote public key to generate DHKey */</span></span><br><span class="line">    HAVE_DHKEY,            <span class="comment">/* DHKey has been calcualted */</span></span><br><span class="line">    SEND_CONFIRM,          <span class="comment">/* Waiting to send Confirm value */</span></span><br><span class="line">    WAIT_NUMBER,           <span class="comment">/* Waiting for number input from user */</span></span><br><span class="line">    WAIT_STRING,           <span class="comment">/* Waiting for string input from user */</span></span><br><span class="line"></span><br><span class="line">    NUM_FLAGS,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_IS_UNICAST(addr)  ((addr) &amp;&amp; (addr) &lt; 0x8000)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_IS_GROUP(addr)    ((addr) &gt;= 0xc000 &amp;&amp; (addr) &lt;= 0xff00)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_IS_VIRTUAL(addr)  ((addr) &gt;= 0x8000 &amp;&amp; (addr) &lt; 0xc000)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_IS_RFU(addr)      ((addr) &gt;= 0xff00 &amp;&amp; (addr) &lt;= 0xfffb)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_UNASSIGNED   0x0000</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_ALL_NODES    0xffff</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_PROXIES      0xfffc</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_FRIENDS      0xfffd</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_ADDR_RELAYS       0xfffe</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_KEY_PRIMARY            0x0000</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_KEY_ANY                0xffff</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_NODE_IDENTITY_STOPPED       0x00</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_NODE_IDENTITY_RUNNING       0x01</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED 0x02</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_IVU_MIN_HOURS      96</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_IVU_HOURS          (BLE_MESH_IVU_MIN_HOURS / CONFIG_BLE_MESH_IVU_DIVIDER)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_IVU_TIMEOUT        K_HOURS(BLE_MESH_IVU_HOURS)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//节点名字最大长度</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_NODE_NAME_SIZE         31</span></span><br><span class="line"><span class="comment">//最大连接节点数</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONFIG_BLE_MESH_MAX_PROV_NODES  5</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* Each node information stored by provisioner */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_node</span> &#123;</span></span><br><span class="line">    <span class="comment">/* Device information */</span></span><br><span class="line">    <span class="type">uint8_t</span>  addr[<span class="number">6</span>];      <span class="comment">/* Node device address */</span></span><br><span class="line">    <span class="type">uint8_t</span>  addr_type;    <span class="comment">/* Node device address type */</span></span><br><span class="line">    <span class="type">uint8_t</span>  dev_uuid[<span class="number">16</span>]; <span class="comment">/* Node Device UUID */</span></span><br><span class="line">    <span class="type">uint16_t</span> oob_info;     <span class="comment">/* Node OOB information */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Provisioning information */</span></span><br><span class="line">    <span class="type">uint16_t</span> unicast_addr; <span class="comment">/* Node unicast address */</span></span><br><span class="line">    <span class="type">uint8_t</span>  element_num;  <span class="comment">/* Node element number */</span></span><br><span class="line">    <span class="type">uint16_t</span> net_idx;      <span class="comment">/* Node NetKey Index */</span></span><br><span class="line">    <span class="type">uint8_t</span>  flags;        <span class="comment">/* Node key refresh flag and iv update flag */</span></span><br><span class="line">    <span class="type">uint32_t</span> iv_index;     <span class="comment">/* Node IV Index */</span></span><br><span class="line">    <span class="type">uint8_t</span>  dev_key[<span class="number">16</span>];  <span class="comment">/* Node device key */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Additional information */</span></span><br><span class="line">    <span class="type">char</span>  name[BLE_MESH_NODE_NAME_SIZE + <span class="number">1</span>]; <span class="comment">/* Node name */</span></span><br><span class="line">    <span class="type">uint16_t</span> comp_length;  <span class="comment">/* Length of Composition Data */</span></span><br><span class="line">    <span class="type">uint8_t</span> *comp_data;    <span class="comment">/* Value of Composition Data */</span></span><br><span class="line">&#125; __packed;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">prov_link</span> &#123;</span></span><br><span class="line">    ATOMIC_DEFINE(flags, NUM_FLAGS);</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_GATT)</span></span><br><span class="line">    <span class="type">bt_mesh_conn_t</span> conn;    <span class="comment">/* GATT connection */</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    <span class="type">uint8_t</span>  uuid[<span class="number">16</span>];       <span class="comment">/* check if device is being provisioned*/</span></span><br><span class="line">    <span class="type">u8_t</span>  dhkey[<span class="number">32</span>];         <span class="comment">/* Calculated DHKey */</span></span><br><span class="line">    <span class="type">u8_t</span>  expect;            <span class="comment">/* Next expected PDU */</span></span><br><span class="line"></span><br><span class="line">    <span class="type">uint8_t</span>  element_num;</span><br><span class="line"></span><br><span class="line">    <span class="type">u8_t</span>  oob_method;</span><br><span class="line">    <span class="type">u8_t</span>  oob_action;</span><br><span class="line">    <span class="type">u8_t</span>  oob_size;</span><br><span class="line"></span><br><span class="line">    <span class="type">u8_t</span>  conf[<span class="number">16</span>];          <span class="comment">/* Remote Confirmation */</span></span><br><span class="line">    <span class="type">u8_t</span>  rand[<span class="number">16</span>];          <span class="comment">/* Local Random */</span></span><br><span class="line">    <span class="type">u8_t</span>  auth[<span class="number">16</span>];          <span class="comment">/* Authentication Value */</span></span><br><span class="line"></span><br><span class="line">    <span class="type">u8_t</span>  conf_salt[<span class="number">16</span>];     <span class="comment">/* ConfirmationSalt */</span></span><br><span class="line">    <span class="type">u8_t</span>  conf_key[<span class="number">16</span>];      <span class="comment">/* ConfirmationKey */</span></span><br><span class="line">    <span class="type">u8_t</span>  conf_inputs[<span class="number">145</span>];  <span class="comment">/* ConfirmationInputs */</span></span><br><span class="line">    <span class="type">u8_t</span>  prov_salt[<span class="number">16</span>];     <span class="comment">/* Provisioning Salt */</span></span><br><span class="line"></span><br><span class="line">    <span class="type">u8_t</span> local_conf[<span class="number">16</span>];        <span class="comment">/* Local Confirmation */</span></span><br><span class="line">    <span class="type">uint16_t</span> assign_addr;       <span class="comment">/* Application assigned address for the device */</span></span><br><span class="line">    <span class="type">uint16_t</span> unicast_addr;      <span class="comment">/* unicast address allocated for device */</span></span><br><span class="line">    <span class="type">uint8_t</span>  ki_flags;          <span class="comment">/* Key refresh flag and iv update flag */</span></span><br><span class="line">    <span class="type">uint32_t</span> iv_index;          <span class="comment">/* IV Index */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line">    <span class="type">u32_t</span>    id;                <span class="comment">/* Link ID */</span></span><br><span class="line">    <span class="type">bool</span>     linking;           <span class="comment">/* Linking is being establishing */</span></span><br><span class="line">    <span class="type">uint16_t</span> send_link_close;   <span class="comment">/* Link close is being sent flag */</span></span><br><span class="line">    <span class="type">uint8_t</span>  pending_ack;       <span class="comment">/* Decide which transaction id ack is pending */</span></span><br><span class="line">    <span class="type">uint8_t</span>  expect_ack_for;    <span class="comment">/* Transaction ACK expected for provisioning pdu */</span></span><br><span class="line">    <span class="type">uint8_t</span>  tx_pdu_type;       <span class="comment">/* The current transmitted Provisioning PDU type */</span></span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">        <span class="type">u8_t</span>  id;        <span class="comment">/* Transaction ID */</span></span><br><span class="line">        <span class="type">u8_t</span>  prev_id;   <span class="comment">/* Previous Transaction ID */</span></span><br><span class="line">        <span class="type">u8_t</span>  seg;       <span class="comment">/* Bit-field of unreceived segments */</span></span><br><span class="line">        <span class="type">u8_t</span>  last_seg;  <span class="comment">/* Last segment (to check length) */</span></span><br><span class="line">        <span class="type">u8_t</span>  fcs;       <span class="comment">/* Expected FCS value */</span></span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span>;</span></span><br><span class="line">    &#125; rx;</span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">        <span class="comment">/* Start timestamp of the transaction */</span></span><br><span class="line">        <span class="type">s64_t</span> start;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Transaction id*/</span></span><br><span class="line">        <span class="type">u8_t</span> id;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Pending outgoing buffer(s) */</span></span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span>[3];</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Retransmit timer */</span></span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">k_delayed_work</span> <span class="title">retransmit</span>;</span></span><br><span class="line">    &#125; tx;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_prov_ctx</span> &#123;</span></span><br><span class="line">    <span class="comment">/* Primary element address of Provisioner */</span></span><br><span class="line">    <span class="type">uint16_t</span> primary_addr;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Provisioning bearers used by Provisioner */</span></span><br><span class="line">    <span class="type">bt_mesh_prov_bearer_t</span> bearers;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Current number of PB-ADV provisioned devices simultaneously */</span></span><br><span class="line">    <span class="type">uint8_t</span>  pba_count;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Current number of PB-GATT provisioned devices simultaneously */</span></span><br><span class="line">    <span class="type">uint8_t</span>  pbg_count;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Current unicast address going to allocated */</span></span><br><span class="line">    <span class="type">uint16_t</span> curr_alloc_addr;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Current net_idx going to be used in provisioning data */</span></span><br><span class="line">    <span class="type">uint16_t</span> curr_net_idx;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Current flags going to be used in provisioning data */</span></span><br><span class="line">    <span class="type">uint8_t</span>  curr_flags;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Current iv_index going to be used in provisioning data */</span></span><br><span class="line">    <span class="type">uint16_t</span> curr_iv_index;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Length of Static OOB value */</span></span><br><span class="line">    <span class="type">uint8_t</span>  static_oob_len;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Static OOB value */</span></span><br><span class="line">    <span class="type">uint8_t</span>  static_oob_val[<span class="number">16</span>];</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Offset of the device uuid to be matched, based on zero */</span></span><br><span class="line">    <span class="type">uint8_t</span>  match_offset;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Length of the device uuid to be matched (start from the match_offset) */</span></span><br><span class="line">    <span class="type">uint8_t</span>  match_length;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Value of the device uuid to be matched */</span></span><br><span class="line">    <span class="type">uint8_t</span>  match_value[<span class="number">16</span>];</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Indicate when received uuid_match adv_pkts, can provision it at once */</span></span><br><span class="line">    <span class="type">bool</span> prov_after_match;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BLE_MESH_PB_ADV)</span></span><br><span class="line">    <span class="comment">/* Mutex used to protect the PB-ADV procedure */</span></span><br><span class="line">    <span class="type">bt_mesh_mutex_t</span> pb_adv_lock;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Mutex used to protect the adv buf during PB-ADV procedure */</span></span><br><span class="line">    <span class="type">bt_mesh_mutex_t</span> pb_buf_lock;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BLE_MESH_PB_GATT)</span></span><br><span class="line">    <span class="comment">/* Mutex used to protect the PB-GATT procedure */</span></span><br><span class="line">    <span class="type">bt_mesh_mutex_t</span> pb_gatt_lock;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">prov_rx</span> &#123;</span></span><br><span class="line">    <span class="type">u32_t</span> link_id;</span><br><span class="line">    <span class="type">u8_t</span>  xact_id;</span><br><span class="line">    <span class="type">u8_t</span>  gpc;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLE_MESH_KEY_UNUSED             0xffff</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONFIG_BLE_MESH_SUBNET_COUNT    3</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT   3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">extern</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_net</span> <span class="title">bt_mesh</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ADV_BUF_SIZE    65</span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">prov_adv_buf</span> &#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> <span class="title">buf</span>;</span></span><br><span class="line">&#125; adv_buf;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">uint8_t</span> adv_buf_data[ADV_BUF_SIZE];</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BT_MESH_PB_ADV */</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_prov_ctx</span> <span class="title">prov_ctx</span>;</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">prov_link</span> <span class="title">link</span>;</span></span><br><span class="line"><span class="type">static</span> <span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_prov</span> *<span class="title">prov</span>;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">uint16_t</span> addr;</span><br><span class="line"><span class="type">static</span> <span class="type">uint32_t</span> seq;</span><br><span class="line"><span class="type">static</span> <span class="type">uint8_t</span> provisoner_devkey[<span class="number">16</span>];</span><br><span class="line"><span class="type">static</span> <span class="type">mesh_netkey_para_t</span> g_netkey;</span><br><span class="line"><span class="type">static</span> <span class="type">mesh_appkey_para_t</span> appkey;</span><br><span class="line"><span class="type">static</span> <span class="type">u32_t</span> seq;</span><br><span class="line"><span class="type">static</span> <span class="type">u16_t</span> addr;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_node</span> <span class="title">mesh_nodes</span>[<span class="title">CONFIG_BLE_MESH_MAX_PROV_NODES</span>];</span></span><br><span class="line"><span class="type">uint8_t</span> filter_uuid[] = &#123;<span class="number">0xa8</span>,<span class="number">0x01</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> RETRANSMIT_TIMEOUT   K_MSEC(500)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BUF_TIMEOUT          K_MSEC(400)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> TRANSACTION_TIMEOUT  K_SECONDS(30)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_GATT)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_BUF_HEADROOM 5</span></span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PROV_BUF_HEADROOM 0</span></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">rx_buf</span> =</span> NET_BUF_SIMPLE(<span class="number">65</span>);</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">close_link</span><span class="params">(<span class="type">u8_t</span> err, <span class="type">u8_t</span> reason)</span>;</span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">free_segments</span><span class="params">(<span class="type">void</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_pb_gatt_close</span><span class="params">(<span class="type">bt_mesh_conn_t</span> conn)</span>;</span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">int</span> <span class="title function_">prov_send</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span>;</span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">send_pub_key</span><span class="params">(<span class="type">uint8_t</span> oob)</span>;</span><br><span class="line"><span class="type">static</span> <span class="keyword">struct</span> net_buf *<span class="title function_">adv_buf_create</span><span class="params">(<span class="type">void</span>)</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//配网器通过网络索引获取网络密钥</span></span><br><span class="line"><span class="type">const</span> <span class="type">uint8_t</span> *<span class="title function_">bt_mesh_provisioner_net_key_get</span><span class="params">(<span class="type">uint16_t</span> net_idx)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_subnet</span> *<span class="title">sub</span> =</span> <span class="literal">NULL</span>;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">    BT_DBG(<span class="string">&quot;%s&quot;</span>, __func__);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; ARRAY_SIZE(bt_mesh.p_sub); i++) &#123;</span><br><span class="line">        sub = bt_mesh.p_sub[i];</span><br><span class="line">        BT_INFO(<span class="string">&quot;sub-&gt;net_idx = %u&quot;</span>,sub-&gt;net_idx);</span><br><span class="line">        <span class="keyword">if</span> (sub &amp;&amp; sub-&gt;net_idx == net_idx) &#123;</span><br><span class="line">            <span class="keyword">if</span> (sub-&gt;kr_flag) &#123;</span><br><span class="line">                <span class="keyword">return</span> sub-&gt;keys[<span class="number">1</span>].net;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> sub-&gt;keys[<span class="number">0</span>].net;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line"><span class="comment">//发送重传</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">buf_sent</span><span class="params">(<span class="type">int</span> err, <span class="type">void</span> *user_data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//如果发送缓存为空，则返回</span></span><br><span class="line">    <span class="keyword">if</span> (!link.tx.buf[<span class="number">0</span>]) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//如果不为空，启动重传</span></span><br><span class="line">    k_delayed_work_submit(&amp;link.tx.retransmit, RETRANSMIT_TIMEOUT);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_send_cb</span> <span class="title">buf_sent_cb</span> =</span> &#123;</span><br><span class="line">    .end = buf_sent,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//初始化缓存区</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_buf_init</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf, <span class="type">u8_t</span> type)</span></span><br><span class="line">&#123;</span><br><span class="line">    net_buf_simple_init(buf, PROV_BUF_HEADROOM);</span><br><span class="line">    net_buf_simple_add_u8(buf, type);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//可靠发送缓存数据</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">send_reliable</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">    link.tx.start = k_uptime_get();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; ARRAY_SIZE(link.tx.buf); i++) &#123;</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span> =</span> link.tx.buf[i];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (i + <span class="number">1</span> &lt; ARRAY_SIZE(link.tx.buf) &amp;&amp; link.tx.buf[i + <span class="number">1</span>]) &#123;</span><br><span class="line">            bt_mesh_adv_send(buf, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            bt_mesh_adv_send(buf, &amp;buf_sent_cb, <span class="literal">NULL</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//取消发送</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_clear_tx</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    k_delayed_work_cancel(&amp;link.tx.retransmit);</span><br><span class="line">	<span class="comment">//清除缓存</span></span><br><span class="line">    free_segments();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//发送PB-ADV广播</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">bearer_ctl_send</span><span class="params">(<span class="type">u8_t</span> op, <span class="type">void</span> *data, <span class="type">u8_t</span> data_len)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span>;</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;发送%s op 0x%02x data_len %u\r\n&quot;</span>,op == <span class="number">0</span> ? <span class="string">&quot;连接&quot;</span>:op == <span class="number">1</span> ? <span class="string">&quot;应答&quot;</span> : op == <span class="number">2</span> ? <span class="string">&quot;断开&quot;</span> : <span class="literal">NULL</span> , op, data_len);</span><br><span class="line"></span><br><span class="line">    prov_clear_tx();</span><br><span class="line"></span><br><span class="line">    buf = adv_buf_create();</span><br><span class="line">    <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">        <span class="keyword">return</span> -ENOBUFS;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//包头填充</span></span><br><span class="line">    net_buf_add_be32(buf, link.id);</span><br><span class="line">    <span class="comment">/* Transaction ID, always 0 for Bearer messages */</span></span><br><span class="line">    net_buf_add_u8(buf, <span class="number">0x00</span>);</span><br><span class="line">    net_buf_add_u8(buf, GPC_CTL(op));</span><br><span class="line">    net_buf_add_mem(buf, data, data_len);</span><br><span class="line"></span><br><span class="line">    link.tx.buf[<span class="number">0</span>] = buf;</span><br><span class="line">    <span class="comment">//可靠发送</span></span><br><span class="line">    send_reliable();</span><br><span class="line">	<span class="comment">//如果是关闭操作还需修改一些其它标志</span></span><br><span class="line">    <span class="keyword">if</span> (op == LINK_CLOSE) &#123;</span><br><span class="line">        <span class="type">uint8_t</span> reason = *(<span class="type">uint8_t</span> *)data;</span><br><span class="line">        link.send_link_close = ((reason &amp; BIT_MASK(<span class="number">2</span>)) &lt;&lt; <span class="number">1</span>) | BIT(<span class="number">0</span>);</span><br><span class="line">        link.tx.id = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//配网器删除节点</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_provisioner_remove_node</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> uuid[<span class="number">16</span>])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">    BT_DBG(<span class="string">&quot;%s&quot;</span>, __func__);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (uuid == <span class="literal">NULL</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid device uuid&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; ARRAY_SIZE(mesh_nodes); i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">memcmp</span>(mesh_nodes[i].dev_uuid, uuid, <span class="number">16</span>)) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">memset</span>(&amp;mesh_nodes[i],<span class="number">0</span>,<span class="keyword">sizeof</span>(mesh_nodes[i]));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//通过UUID找到节点</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">struct</span> bt_mesh_node *<span class="title function_">provisioner_find_node_with_uuid</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> uuid[<span class="number">16</span>], <span class="type">uint16_t</span> *index)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">    BT_DBG(<span class="string">&quot;%s&quot;</span>, __func__);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (uuid == <span class="literal">NULL</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid device uuid&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; ARRAY_SIZE(mesh_nodes); i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="built_in">memcmp</span>(mesh_nodes[i].dev_uuid, uuid, <span class="number">16</span>)) &#123;</span><br><span class="line">            <span class="keyword">if</span> (index) &#123;</span><br><span class="line">                *index = i;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> &amp;mesh_nodes[i];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//节点是否连接</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">node_is_connected</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> *uuid)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span>(uuid == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;UUID错误\r\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; CONFIG_BLE_MESH_MAX_PROV_NODES;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">memcmp</span>(&amp;mesh_nodes[i].dev_uuid[<span class="number">0</span>],uuid,<span class="number">16</span>) == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;已经建立过连接\r\n&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//广播包过滤uuid</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">adv_filter_uuid</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> * uuid)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span>(uuid == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;uuid 错误\r\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">memcmp</span>(filter_uuid,uuid,<span class="keyword">sizeof</span>(filter_uuid)) == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;找到未配网设备\r\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//获得网络层缓存区</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">struct</span> net_buf_simple *<span class="title function_">bt_mesh_pba_get_buf</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> &amp;adv_buf.buf;</span><br><span class="line"></span><br><span class="line">    buf-&gt;len  = <span class="number">0</span>;</span><br><span class="line">    buf-&gt;data = adv_buf_data;</span><br><span class="line">    *(&amp;buf-&gt;__buf[<span class="number">0</span>]) = adv_buf_data;</span><br><span class="line">    buf-&gt;size = <span class="number">65</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> buf;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//建立连接</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">provisioner_link_open</span><span class="params">(<span class="type">const</span> <span class="type">uint8_t</span> * uuid)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//先判断UUID是否合法</span></span><br><span class="line">    <span class="keyword">if</span>(node_is_connected(uuid) || adv_filter_uuid(uuid))</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> ;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(atomic_test_bit(link.flags, LINK_ACTIVE))</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;已经在配网阶段\r\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;生成随机数\r\n&quot;</span>);</span><br><span class="line">    <span class="comment">//随机生成一个连接ID</span></span><br><span class="line">    bt_mesh_rand(&amp;link.id,<span class="keyword">sizeof</span>(link.id));</span><br><span class="line">    <span class="comment">//发起配网操作</span></span><br><span class="line">    bearer_ctl_send(LINK_OPEN,(<span class="type">void</span> *)uuid, <span class="number">16</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;发起配网\r\n&quot;</span>);</span><br><span class="line">    <span class="comment">//应用层触发开始配网事件</span></span><br><span class="line">    genie_event(GENIE_EVT_SDK_MESH_PROV_START,&amp;link.id);</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Set LINK_ACTIVE just to be in compatibility with current Zephyr code */</span></span><br><span class="line">    atomic_set_bit(link.flags, LINK_ACTIVE);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;link_open) &#123;</span><br><span class="line">        prov-&gt;link_open(BT_MESH_PROV_ADV);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    link.expect = PROV_INVITE;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//发送邀请</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">send_invite</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//实例化网络层缓存buf，实际为局部结构体变量</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">2</span>);</span><br><span class="line">	<span class="comment">//初始化缓存头部类型</span></span><br><span class="line">    prov_buf_init(buf, PROV_INVITE);</span><br><span class="line">    <span class="comment">//添加连接超时</span></span><br><span class="line">    net_buf_simple_add_u8(buf, <span class="number">0</span>);</span><br><span class="line">    <span class="comment">//等待连接时间记录到本地</span></span><br><span class="line">    link.conf_inputs[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//发送缓存</span></span><br><span class="line">    <span class="keyword">if</span> (prov_send(buf)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to send Provisioning Invite&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_NVAL_PDU, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;已发出邀请\r\n&quot;</span>);</span><br><span class="line">    <span class="comment">//下次预期接收配网能力</span></span><br><span class="line">    link.expect = PROV_CAPABILITIES;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//释放实例化缓存</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">free_segments</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; ARRAY_SIZE(link.tx.buf); i++) &#123;</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span> =</span> link.tx.buf[i];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        link.tx.buf[i] = <span class="literal">NULL</span>;</span><br><span class="line">        <span class="comment">/* Mark as canceled */</span></span><br><span class="line">        BT_MESH_ADV(buf)-&gt;busy = <span class="number">0</span>;</span><br><span class="line">        net_buf_unref(buf);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//复位连接</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">reset_link</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    prov_clear_tx();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;link_close) &#123;</span><br><span class="line">        prov-&gt;link_close(BT_MESH_PROV_ADV);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Clear everything except the retransmit delayed work config */</span></span><br><span class="line">    <span class="built_in">memset</span>(&amp;link, <span class="number">0</span>, offsetof(<span class="keyword">struct</span> prov_link, tx.retransmit));</span><br><span class="line"></span><br><span class="line">    link.rx.prev_id = XACT_NVAL;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_pub_key_get()) &#123;</span><br><span class="line">        atomic_set_bit(link.flags, LOCAL_PUB_KEY);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_GATT)</span></span><br><span class="line">    link.rx.buf = bt_mesh_proxy_get_buf();</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">    net_buf_simple_init(rx_buf, <span class="number">0</span>);</span><br><span class="line">    link.rx.buf = rx_buf;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Disable Attention Timer if it was set */</span></span><br><span class="line">    <span class="keyword">if</span> (link.conf_inputs[<span class="number">0</span>]) &#123;</span><br><span class="line">        bt_mesh_attention(<span class="literal">NULL</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//创建广播</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">struct</span> net_buf *<span class="title function_">adv_buf_create</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span>;</span></span><br><span class="line"></span><br><span class="line">    buf = bt_mesh_adv_create(BT_MESH_ADV_PROV, PROV_XMIT_COUNT,</span><br><span class="line">                             PROV_XMIT_INT, BUF_TIMEOUT);</span><br><span class="line">    <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Out of provisioning buffers&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> buf;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">u8_t</span> pending_ack = XACT_NVAL;</span><br><span class="line"><span class="comment">//响应完成回调用</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">ack_complete</span><span class="params">(<span class="type">u16_t</span> duration, <span class="type">int</span> err, <span class="type">void</span> *user_data)</span></span><br><span class="line">&#123;</span><br><span class="line">    BT_DBG(<span class="string">&quot;xact %u complete&quot;</span>, (<span class="type">u8_t</span>)pending_ack);</span><br><span class="line">    pending_ack = XACT_NVAL;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//发送响应</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">gen_prov_ack_send</span><span class="params">(<span class="type">u8_t</span> xact_id)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">static</span> <span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_send_cb</span> <span class="title">cb</span> =</span> &#123;</span><br><span class="line">        .start = ack_complete,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_send_cb</span> *<span class="title">complete</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span>;</span></span><br><span class="line"></span><br><span class="line">    BT_DBG(<span class="string">&quot;pending_ack = %u,xact_id %u&quot;</span>,pending_ack,xact_id);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (pending_ack == xact_id) &#123;</span><br><span class="line">        BT_DBG(<span class="string">&quot;Not sending duplicate ack&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//创建广播包缓存</span></span><br><span class="line">    buf = adv_buf_create();</span><br><span class="line">    <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (pending_ack == XACT_NVAL) &#123;</span><br><span class="line">        pending_ack = xact_id;</span><br><span class="line">        complete = &amp;cb;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        complete = <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    net_buf_add_be32(buf, link.id);</span><br><span class="line">    net_buf_add_u8(buf, xact_id);</span><br><span class="line">    net_buf_add_u8(buf, GPC_ACK);</span><br><span class="line">	<span class="comment">//发送响应广播</span></span><br><span class="line">    bt_mesh_adv_send(buf, complete, <span class="literal">NULL</span>);</span><br><span class="line">    <span class="comment">//释放缓存</span></span><br><span class="line">    net_buf_unref(buf);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//计算最后包号</span></span><br><span class="line"><span class="type">static</span> <span class="type">u8_t</span> <span class="title function_">last_seg</span><span class="params">(<span class="type">u8_t</span> len)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//如果小于20个字节 就一包数据</span></span><br><span class="line">    <span class="keyword">if</span> (len &lt;= START_PAYLOAD_MAX) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//否则超过20个字节就要分包（len -= 20）</span></span><br><span class="line">    len -= START_PAYLOAD_MAX;</span><br><span class="line">	<span class="comment">//一包数据传23个字节（第一包额外加了一些包头所以是20个字节）</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span> + (len / CONT_PAYLOAD_MAX);   <span class="comment">// 1 + （len / 23）</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//下一个传输编号</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">u8_t</span> <span class="title function_">next_transaction_id</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (link.tx.id &gt; <span class="number">0x7F</span>) &#123;</span><br><span class="line">        link.tx.id = <span class="number">0x0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> link.tx.id++;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//发送广播</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">prov_send_adv</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *msg)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">start</span>, *<span class="title">buf</span>;</span></span><br><span class="line">    <span class="type">u8_t</span> seg_len, seg_id;</span><br><span class="line">    <span class="type">u8_t</span> xact_id;</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line">    <span class="comment">//PROV_D(&quot;len %u: %s&quot;, msg-&gt;len, bt_hex(msg-&gt;data, msg-&gt;len));</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//清除发送缓存</span></span><br><span class="line">    prov_clear_tx();</span><br><span class="line">	<span class="comment">//创建广播包缓存</span></span><br><span class="line">    start = adv_buf_create();</span><br><span class="line">    <span class="keyword">if</span> (!start) &#123;</span><br><span class="line">        <span class="keyword">return</span> -ENOBUFS;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//下一个传输编号</span></span><br><span class="line">    xact_id = next_transaction_id();</span><br><span class="line">    net_buf_add_be32(start, link.id);</span><br><span class="line">    net_buf_add_u8(start, xact_id);</span><br><span class="line"></span><br><span class="line">    net_buf_add_u8(start, GPC_START(last_seg(msg-&gt;len)));</span><br><span class="line">    net_buf_add_be16(start, msg-&gt;len);</span><br><span class="line">    net_buf_add_u8(start, bt_mesh_fcs_calc(msg-&gt;data, msg-&gt;len));</span><br><span class="line"></span><br><span class="line">    link.tx.buf[<span class="number">0</span>] = start;</span><br><span class="line"></span><br><span class="line">    seg_len = min(msg-&gt;len, START_PAYLOAD_MAX);</span><br><span class="line"><span class="comment">//    printf(&quot;seg 0 len %u: %s&quot;, seg_len, bt_hex(msg-&gt;data, seg_len));</span></span><br><span class="line">    net_buf_add_mem(start, msg-&gt;data, seg_len);</span><br><span class="line">    net_buf_simple_pull(msg, seg_len);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;发送: 传输标号 = %u\r\n&quot;</span>,xact_id);</span><br><span class="line">    buf = start;</span><br><span class="line">    <span class="comment">//将所以数据写入缓存</span></span><br><span class="line">    <span class="keyword">for</span> (seg_id = <span class="number">1</span>; msg-&gt;len &gt; <span class="number">0</span>; seg_id++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (seg_id &gt;= ARRAY_SIZE(link.tx.buf)) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Too big message&quot;</span>);</span><br><span class="line">            free_segments();</span><br><span class="line">            <span class="keyword">return</span> -E2BIG;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        buf = adv_buf_create();</span><br><span class="line">        <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">            free_segments();</span><br><span class="line">            <span class="keyword">return</span> -ENOBUFS;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        link.tx.buf[seg_id] = buf;</span><br><span class="line"></span><br><span class="line">        seg_len = min(msg-&gt;len, CONT_PAYLOAD_MAX);</span><br><span class="line"></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;seg_id %u len %u: %s\r\n&quot;</span>, seg_id, seg_len,</span><br><span class="line">               bt_hex(msg-&gt;data, seg_len));</span><br><span class="line"></span><br><span class="line">        net_buf_add_be32(buf, link.id);</span><br><span class="line">        net_buf_add_u8(buf, xact_id);</span><br><span class="line">        net_buf_add_u8(buf, GPC_CONT(seg_id));</span><br><span class="line">        net_buf_add_mem(buf, msg-&gt;data, seg_len);</span><br><span class="line">        net_buf_simple_pull(msg, seg_len);</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//可靠发送</span></span><br><span class="line">    send_reliable();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BT_MESH_PB_ADV */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_GATT)</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">prov_send_gatt</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *msg)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (!link.conn) &#123;</span><br><span class="line">        <span class="keyword">return</span> -ENOTCONN;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> bt_mesh_proxy_send(link.conn, BT_MESH_PROXY_PROV, msg);</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BT_MESH_PB_GATT */</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">int</span> <span class="title function_">prov_send</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_GATT)</span></span><br><span class="line">    <span class="keyword">if</span> (link.conn) &#123;</span><br><span class="line">        <span class="keyword">return</span> prov_send_gatt(buf);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line">    <span class="comment">//进入到这里发送</span></span><br><span class="line">    <span class="keyword">return</span> prov_send_adv(buf);</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//发送配网失败消息</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_send_fail_msg</span><span class="params">(<span class="type">u8_t</span> err)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">    prov_buf_init(buf, PROV_FAILED);</span><br><span class="line">    net_buf_simple_add_u8(buf, err);</span><br><span class="line">    prov_send(buf);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_invite</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;prov_invite\r\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_capabilities</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;接收到配网能力\r\n&quot;</span>);</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">6</span>);</span><br><span class="line">    <span class="type">uint16_t</span> algorithms = <span class="number">0U</span>, output_action = <span class="number">0U</span>, input_action = <span class="number">0U</span>;</span><br><span class="line">    <span class="type">uint8_t</span>  element_num = <span class="number">0U</span>, pub_key_oob = <span class="number">0U</span>, static_oob = <span class="number">0U</span>,</span><br><span class="line">             output_size = <span class="number">0U</span>, input_size = <span class="number">0U</span>;</span><br><span class="line">    <span class="type">uint8_t</span>  auth_method = <span class="number">0U</span>, auth_action = <span class="number">0U</span>, auth_size = <span class="number">0U</span>;</span><br><span class="line"></span><br><span class="line">    element_num = data[<span class="number">0</span>];</span><br><span class="line">    BT_INFO(<span class="string">&quot;Elements:          0x%02x&quot;</span>, element_num);</span><br><span class="line">    <span class="keyword">if</span> (!element_num) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid element number %d&quot;</span>, element_num);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//元素个数</span></span><br><span class="line">    link.element_num = element_num;</span><br><span class="line"></span><br><span class="line">    algorithms = sys_get_be16(&amp;data[<span class="number">1</span>]);</span><br><span class="line">    BT_INFO(<span class="string">&quot;Algorithms:        0x%04x&quot;</span>, algorithms);</span><br><span class="line">    <span class="keyword">if</span> (algorithms != BIT(PROV_ALG_P256)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid algorithms 0x%04x&quot;</span>, algorithms);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//配网能力标志检查</span></span><br><span class="line">    pub_key_oob = data[<span class="number">3</span>];</span><br><span class="line">    BT_INFO(<span class="string">&quot;Public Key Type:   0x%02x&quot;</span>, pub_key_oob);</span><br><span class="line">    <span class="keyword">if</span> (pub_key_oob &gt; <span class="number">0x01</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid public key type 0x%02x&quot;</span>, pub_key_oob);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    pub_key_oob = ((prov-&gt;prov_pub_key_oob &amp;&amp;</span><br><span class="line">                    prov-&gt;prov_pub_key_oob_cb) ? pub_key_oob : <span class="number">0x00</span>);</span><br><span class="line">    </span><br><span class="line">    static_oob = data[<span class="number">4</span>];</span><br><span class="line">    BT_INFO(<span class="string">&quot;Static OOB Type:   0x%02x&quot;</span>, static_oob);</span><br><span class="line">    <span class="keyword">if</span> (static_oob &gt; <span class="number">0x01</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid Static OOB type 0x%02x&quot;</span>, static_oob);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">    static_oob = (prov_ctx.static_oob_len ? static_oob : <span class="number">0x00</span>);</span><br><span class="line"></span><br><span class="line">    output_size = data[<span class="number">5</span>];</span><br><span class="line">    BT_INFO(<span class="string">&quot;Output OOB Size:   0x%02x&quot;</span>, output_size);</span><br><span class="line">    <span class="keyword">if</span> (output_size &gt; <span class="number">0x08</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid Output OOB size %d&quot;</span>, output_size);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    output_action = sys_get_be16(&amp;data[<span class="number">6</span>]);</span><br><span class="line">    BT_INFO(<span class="string">&quot;Output OOB Action: 0x%04x&quot;</span>, output_action);</span><br><span class="line">    <span class="keyword">if</span> (output_action &gt; <span class="number">0x1f</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid Output OOB action 0x%04x&quot;</span>, output_action);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Provisioner select output action */</span></span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;prov_input_num &amp;&amp; output_size) &#123;</span><br><span class="line">        output_action = __builtin_ctz(output_action);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        output_size = <span class="number">0x0</span>;</span><br><span class="line">        output_action = <span class="number">0x0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    input_size = data[<span class="number">8</span>];</span><br><span class="line">    BT_INFO(<span class="string">&quot;Input OOB Size:    0x%02x&quot;</span>, input_size);</span><br><span class="line">    <span class="keyword">if</span> (input_size &gt; <span class="number">0x08</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid Input OOB size %d&quot;</span>, input_size);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    input_action = sys_get_be16(&amp;data[<span class="number">9</span>]);</span><br><span class="line">    BT_INFO(<span class="string">&quot;Input OOB Action:  0x%04x&quot;</span>, input_action);</span><br><span class="line">    <span class="keyword">if</span> (input_action &gt; <span class="number">0x0f</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid Input OOB action 0x%04x&quot;</span>, input_action);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 0</span></span><br><span class="line">    <span class="comment">/* Make sure received pdu is ok and cancel the timeout timer */</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_atomic_test_and_clear_bit(link.flags, TIMEOUT_START)) &#123;</span><br><span class="line">        k_delayed_work_cancel(&amp;link.timeout);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Provisioner select input action */</span></span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;prov_output_num &amp;&amp; input_size) &#123;</span><br><span class="line">        input_action = __builtin_ctz(input_action);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        input_size = <span class="number">0x0</span>;</span><br><span class="line">        input_action = <span class="number">0x0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (static_oob) &#123;</span><br><span class="line">        <span class="comment">/* if static oob is valid, just use static oob */</span></span><br><span class="line">        auth_method = AUTH_METHOD_STATIC;</span><br><span class="line">        auth_action = <span class="number">0x00</span>;</span><br><span class="line">        auth_size   = <span class="number">0x00</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (!output_size &amp;&amp; !input_size) &#123;</span><br><span class="line">            auth_method = AUTH_METHOD_NO_OOB;</span><br><span class="line">            auth_action = <span class="number">0x00</span>;</span><br><span class="line">            auth_size   = <span class="number">0x00</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (!output_size &amp;&amp; input_size) &#123;</span><br><span class="line">            auth_method = AUTH_METHOD_INPUT;</span><br><span class="line">            auth_action = (<span class="type">uint8_t</span>)input_action;</span><br><span class="line">            auth_size   = input_size;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            auth_method = AUTH_METHOD_OUTPUT;</span><br><span class="line">            auth_action = (<span class="type">uint8_t</span>)output_action;</span><br><span class="line">            auth_size   = output_size;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Store provisioning capabilities value in conf_inputs */</span></span><br><span class="line">    <span class="comment">//保存要发送的参数到 conf_inputs 里，以便后期用来生成“配网佐料”</span></span><br><span class="line">    <span class="built_in">memcpy</span>(&amp;link.conf_inputs[<span class="number">1</span>], data, <span class="number">11</span>);</span><br><span class="line">    <span class="comment">//填充配网阶段参数</span></span><br><span class="line">    prov_buf_init(buf, PROV_START);</span><br><span class="line">    net_buf_simple_add_u8(buf, prov-&gt;prov_algorithm);</span><br><span class="line">    net_buf_simple_add_u8(buf, pub_key_oob);</span><br><span class="line">    net_buf_simple_add_u8(buf, auth_method);</span><br><span class="line">    net_buf_simple_add_u8(buf, auth_action);</span><br><span class="line">    net_buf_simple_add_u8(buf, auth_size);</span><br><span class="line">    <span class="comment">//复制到认证数据里计算认证时用</span></span><br><span class="line">    <span class="built_in">memcpy</span>(&amp;link.conf_inputs[<span class="number">12</span>], &amp;buf-&gt;data[<span class="number">1</span>], <span class="number">5</span>);</span><br><span class="line">    <span class="comment">//发送provsisioning start</span></span><br><span class="line">    <span class="keyword">if</span> (prov_send(buf)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to send Provisioning Start&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    link.oob_method = auth_method;</span><br><span class="line">    link.oob_action = auth_action;</span><br><span class="line">    link.oob_size   = auth_size;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** After prov start sent, use OOB to get remote public key.</span></span><br><span class="line"><span class="comment">     *  And we just follow the procedure in Figure 5.15 of Section</span></span><br><span class="line"><span class="comment">     *  5.4.2.3 of Mesh Profile Spec.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 1</span></span><br><span class="line">    <span class="keyword">if</span> (pub_key_oob) &#123;</span><br><span class="line">        <span class="keyword">if</span> (prov-&gt;prov_pub_key_oob_cb()) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Failed to notify input OOB Public Key&quot;</span>);</span><br><span class="line">            <span class="keyword">goto</span> fail;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/** If using PB-ADV, need to listen for transaction ack,</span></span><br><span class="line"><span class="comment">     *  after ack is received, provisioner can send public key.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line"></span><br><span class="line">    link.expect_ack_for = PROV_START;</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BT_MESH_PB_ADV */</span></span></span><br><span class="line">    <span class="comment">//发送交换公钥</span></span><br><span class="line">    send_pub_key(pub_key_oob);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">fail:</span><br><span class="line">    close_link(PROV_ERR_CFM_FAILED,CLOSE_REASON_FAILED);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//oob认证数据生成（无oob只能填充0）</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">prov_auth</span><span class="params">(<span class="type">u8_t</span> method, <span class="type">u8_t</span> action, <span class="type">u8_t</span> size)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">switch</span> (method) &#123;</span><br><span class="line">        <span class="keyword">case</span> AUTH_METHOD_STATIC:</span><br><span class="line">            <span class="keyword">if</span> (action || size) &#123;</span><br><span class="line">                <span class="keyword">return</span> -EINVAL;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">memcpy</span>(link.auth + <span class="number">16</span> - prov-&gt;static_val_len,</span><br><span class="line">                   prov-&gt;static_val, prov-&gt;static_val_len);</span><br><span class="line">            <span class="built_in">memset</span>(link.auth, <span class="number">0</span>, <span class="keyword">sizeof</span>(link.auth) - prov-&gt;static_val_len);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="comment">//只支持无OOB</span></span><br><span class="line">        <span class="keyword">case</span> AUTH_METHOD_NO_OOB:</span><br><span class="line">            <span class="keyword">if</span> (action || size) &#123;</span><br><span class="line">                <span class="keyword">return</span> -EINVAL;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="built_in">memset</span>(link.auth, <span class="number">0</span>, <span class="keyword">sizeof</span>(link.auth));</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">case</span> AUTH_METHOD_OUTPUT:</span><br><span class="line">        <span class="keyword">case</span> AUTH_METHOD_INPUT:</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">return</span> -EINVAL;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_start</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;prov_start\r\n&quot;</span>);   </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//发送认证</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">send_confirm</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">17</span>);</span><br><span class="line">    <span class="type">uint8_t</span> *conf = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;ConfInputs[0]   %s&quot;</span>, bt_hex(link.conf_inputs, <span class="number">64</span>));</span><br><span class="line">    BT_INFO(<span class="string">&quot;ConfInputs[64]  %s&quot;</span>, bt_hex(&amp;link.conf_inputs[<span class="number">64</span>], <span class="number">64</span>));</span><br><span class="line">    BT_INFO(<span class="string">&quot;ConfInputs[128] %s&quot;</span>, bt_hex(&amp;link.conf_inputs[<span class="number">128</span>], <span class="number">17</span>));</span><br><span class="line">	<span class="comment">//生成Confirmation salt  认证佐料</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_prov_conf_salt(link.conf_inputs, link.conf_salt)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Unable to generate confirmation salt&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;ConfirmationSalt: %s&quot;</span>, bt_hex(link.conf_salt, <span class="number">16</span>));</span><br><span class="line">	<span class="comment">//共享密钥和认证佐料生成认证密钥</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_prov_conf_key(link.dhkey, link.conf_salt, link.conf_key)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Unable to generate confirmation key&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;ConfirmationKey: %s&quot;</span>, bt_hex(link.conf_key, <span class="number">16</span>));</span><br><span class="line">	<span class="comment">//生成随机值</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_rand(link.rand, <span class="number">16</span>)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Unable to generate random number&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;LocalRandom: %s&quot;</span>, bt_hex(link.rand, <span class="number">16</span>));</span><br><span class="line"></span><br><span class="line">    prov_buf_init(buf, PROV_CONFIRM);</span><br><span class="line"></span><br><span class="line">    conf = net_buf_simple_add(buf, <span class="number">16</span>);</span><br><span class="line">    <span class="comment">//认证密钥、随机值和oob数据（这里为0）生成确认值直接写入到了buf</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_prov_conf(link.conf_key, link.rand, link.auth,conf)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Unable to generate confirmation value&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//拷贝确认值到本地存储</span></span><br><span class="line">    <span class="built_in">memcpy</span>(link.local_conf, conf, <span class="number">16</span>);</span><br><span class="line">    <span class="comment">//打印要发送得认证数据</span></span><br><span class="line">    BT_INFO(<span class="string">&quot;********************************************&quot;</span>);</span><br><span class="line">    BT_INFO(<span class="string">&quot;link.conf_key: %s&quot;</span>, bt_hex(link.conf_key, <span class="number">16</span>));</span><br><span class="line">    BT_INFO(<span class="string">&quot;link.rand: %s&quot;</span>, bt_hex(link.rand, <span class="number">16</span>));</span><br><span class="line">    BT_INFO(<span class="string">&quot;conf_verify: %s&quot;</span>, bt_hex(conf, <span class="number">16</span>));</span><br><span class="line">    BT_INFO(<span class="string">&quot;********************************************&quot;</span>);</span><br><span class="line">    <span class="comment">//发送确认值</span></span><br><span class="line">    <span class="keyword">if</span> (prov_send(buf)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to send Provisioning Confirm&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_RESOURCES, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    link.expect = PROV_CONFIRM;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//共享密钥生成回调函数</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_dh_key_cb</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> key[<span class="number">32</span>])</span></span><br><span class="line">&#123;</span><br><span class="line">    BT_INFO(<span class="string">&quot;DHkey 地址：%p&quot;</span>, key);</span><br><span class="line">    <span class="keyword">if</span> (!key) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;DHKey generation failed&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    sys_memcpy_swap(link.dhkey, key, <span class="number">32</span>);</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;DHkey: %s&quot;</span>, bt_hex(link.dhkey, <span class="number">32</span>));</span><br><span class="line"></span><br><span class="line">    atomic_set_bit(link.flags, HAVE_DHKEY);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (prov_auth(link.oob_method,</span><br><span class="line">                  link.oob_action, link.oob_size) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to authenticate&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (link.oob_method == AUTH_METHOD_OUTPUT ||</span><br><span class="line">            link.oob_method == AUTH_METHOD_INPUT) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//如果下次预期的PDU不是输入比较，那就发送认证值</span></span><br><span class="line">    <span class="keyword">if</span> (link.expect != PROV_INPUT_COMPLETE) &#123;</span><br><span class="line">        send_confirm();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//生成共享密钥</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_gen_dh_key</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> pub_key[<span class="number">64</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Copy device public key in little-endian for bt_mesh_dh_key_gen().</span></span><br><span class="line"><span class="comment">     * X and Y halves are swapped independently.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    sys_memcpy_swap(&amp;pub_key[<span class="number">0</span>], &amp;link.conf_inputs[<span class="number">81</span>], <span class="number">32</span>);</span><br><span class="line">    sys_memcpy_swap(&amp;pub_key[<span class="number">32</span>], &amp;link.conf_inputs[<span class="number">113</span>], <span class="number">32</span>);</span><br><span class="line">    <span class="comment">//Diffie-Hellman Key</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_dh_key_gen(pub_key, prov_dh_key_cb)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to generate DHKey&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//发送公钥</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">send_pub_key</span><span class="params">(<span class="type">uint8_t</span> oob)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">65</span>);</span><br><span class="line">    <span class="type">const</span> <span class="type">u8_t</span> *key;</span><br><span class="line"></span><br><span class="line">    key = bt_mesh_pub_key_get();</span><br><span class="line">    <span class="keyword">if</span> (!key) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;No public key available&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_RESOURCES, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    BT_INFO(<span class="string">&quot;Local Public Key: %s\r\n&quot;</span>, bt_hex(key, <span class="number">64</span>));</span><br><span class="line">    atomic_set_bit(link.flags, LOCAL_PUB_KEY);</span><br><span class="line">    prov_buf_init(buf, PROV_PUB_KEY);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Swap X and Y halves independently to big-endian */</span></span><br><span class="line">    sys_memcpy_swap(net_buf_simple_add(buf, <span class="number">32</span>), key, <span class="number">32</span>);</span><br><span class="line">    sys_memcpy_swap(net_buf_simple_add(buf, <span class="number">32</span>), &amp;key[<span class="number">32</span>], <span class="number">32</span>);</span><br><span class="line">    <span class="comment">//存储公钥到conf_inputs里</span></span><br><span class="line">    <span class="built_in">memcpy</span>(&amp;link.conf_inputs[<span class="number">17</span>], &amp;buf-&gt;data[<span class="number">1</span>], <span class="number">64</span>);</span><br><span class="line"></span><br><span class="line">    prov_send(buf);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!oob) &#123;</span><br><span class="line">        BT_INFO(<span class="string">&quot;进入!oob link.expect = PROV_PUB_KEY&quot;</span>);</span><br><span class="line">        link.expect = PROV_PUB_KEY;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        BT_INFO(<span class="string">&quot;没有进入link.expect = PROV_PUB_KEY&quot;</span>);</span><br><span class="line">        <span class="comment">/** Have already got device public key. If next is to</span></span><br><span class="line"><span class="comment">         *  send confirm(not wait for input complete), need to</span></span><br><span class="line"><span class="comment">         *  wait for transactiona ack for public key then send</span></span><br><span class="line"><span class="comment">         *  provisioning confirm pdu.</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BLE_MESH_PB_ADV)</span></span><br><span class="line"><span class="comment">//        if (idx &lt; CONFIG_BLE_MESH_PBA_SAME_TIME) &#123;</span></span><br><span class="line"><span class="comment">//            link.expect_ack_for = PROV_PUB_KEY;</span></span><br><span class="line"><span class="comment">//            return;</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BLE_MESH_PB_ADV */</span></span></span><br><span class="line"></span><br><span class="line">        <span class="comment">/* If remote public key has been read, then start to generate DHkey,</span></span><br><span class="line"><span class="comment">         * otherwise wait for device oob public key.</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">if</span> (atomic_test_bit(link.flags, REMOTE_PUB_KEY)) &#123;</span><br><span class="line">            prov_gen_dh_key();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            atomic_set_bit(link.flags, WAIT_GEN_DHKEY);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_pub_key</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    BT_INFO(<span class="string">&quot;Remote Public Key: %s&quot;</span>, bt_hex(data, <span class="number">64</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">//拷贝对方设备公钥</span></span><br><span class="line">    <span class="built_in">memcpy</span>(&amp;link.conf_inputs[<span class="number">81</span>], data, <span class="number">64</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!atomic_test_bit(link.flags, LOCAL_PUB_KEY)) &#123;</span><br><span class="line">        <span class="comment">/* Clear retransmit timer */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line">        prov_clear_tx();</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">        atomic_set_bit(link.flags, REMOTE_PUB_KEY);</span><br><span class="line">        BT_WARN(<span class="string">&quot;Waiting for local public key&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    prov_gen_dh_key();</span><br><span class="line">    <span class="comment">//send_pub_key();</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_input_complete</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    send_confirm();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//收到对方确认值进入</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_confirm</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    BT_INFO(<span class="string">&quot;Remote Confirm: %s&quot;</span>, bt_hex(data, <span class="number">16</span>));</span><br><span class="line">    </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">17</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* <span class="doctag">NOTE:</span> The Bluetooth SIG recommends that potentially vulnerable mesh provisioners</span></span><br><span class="line"><span class="comment">     * restrict the authentication procedure and not accept provisioning random and</span></span><br><span class="line"><span class="comment">     * provisioning confirmation numbers from a remote peer that are the same as those</span></span><br><span class="line"><span class="comment">     * selected by the local device (CVE-2020-26560).</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//本地确认值和对方 确认值要不一样</span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">memcmp</span>(data, link.local_conf, <span class="number">16</span>)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Confirmation value is identical to ours, rejecting.&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="built_in">memcpy</span>(link.conf, data, <span class="number">16</span>);</span><br><span class="line">	<span class="comment">//如果标志位已经有共享密钥，发送认证值标志位也置位</span></span><br><span class="line">    <span class="keyword">if</span> (atomic_test_bit(link.flags, HAVE_DHKEY)) &#123;</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BLE_MESH_PB_ADV)</span></span><br><span class="line">        prov_clear_tx(idx);</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">        atomic_set_bit(link.flags, SEND_CONFIRM);</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//发送随机值</span></span><br><span class="line">    prov_buf_init(buf, PROV_RANDOM);</span><br><span class="line"></span><br><span class="line">    net_buf_simple_add_mem(buf, link.rand, <span class="number">16</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (prov_send(buf)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to send Provisioning Random&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_CFM_FAILED, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    link.expect = PROV_RANDOM;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//发送配网数据</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">send_prov_data</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">net_buf_simple</span> *<span class="title">buf</span> =</span> PROV_BUF(<span class="number">34</span>);</span><br><span class="line">    <span class="type">uint16_t</span> prev_addr = <span class="number">0</span>;</span><br><span class="line">    <span class="type">uint16_t</span> max_addr = <span class="number">0</span>;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_node</span> *<span class="title">node</span> =</span> <span class="literal">NULL</span>;</span><br><span class="line">    <span class="type">const</span> <span class="type">uint8_t</span> *netkey = <span class="literal">NULL</span>;</span><br><span class="line">    <span class="type">uint8_t</span> session_key[<span class="number">16</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">    <span class="type">uint8_t</span> nonce[<span class="number">13</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">    <span class="type">uint8_t</span> pdu[<span class="number">25</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">    <span class="type">int</span> err = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//生成会话密钥</span></span><br><span class="line">    err = bt_mesh_session_key(link.dhkey, link.prov_salt, session_key);</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to generate session key&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">    BT_INFO(<span class="string">&quot;SessionKey: %s&quot;</span>, bt_hex(session_key, <span class="number">16</span>));</span><br><span class="line">	<span class="comment">//生成一次性随机值</span></span><br><span class="line">    err = bt_mesh_prov_nonce(link.dhkey, link.prov_salt, nonce);</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to generate session nonce&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">    BT_INFO(<span class="string">&quot;Nonce: %s&quot;</span>, bt_hex(nonce, <span class="number">13</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Assign provisioning data for the device. Currently all provisioned devices</span></span><br><span class="line"><span class="comment">     * will be added to the primary subnet, and may add an API to choose to which</span></span><br><span class="line"><span class="comment">     * subnet will the device be provisioned later.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 1</span></span><br><span class="line">    <span class="comment">//获得网络密钥</span></span><br><span class="line">    netkey = bt_mesh_provisioner_net_key_get(prov_ctx.curr_net_idx);</span><br><span class="line">    BT_INFO(<span class="string">&quot;获取网络密钥 netkey: %p&quot;</span>,netkey);</span><br><span class="line">    <span class="keyword">if</span> (!netkey) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;No NetKey for provisioning data&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">memcpy</span>(pdu, netkey, <span class="number">16</span>);</span><br><span class="line">    sys_put_be16(prov_ctx.curr_net_idx, &amp;pdu[<span class="number">16</span>]);</span><br><span class="line">    pdu[<span class="number">18</span>] = prov_ctx.curr_flags;</span><br><span class="line">    sys_put_be32(prov_ctx.curr_iv_index, &amp;pdu[<span class="number">19</span>]);</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The Provisioner must not reuse unicast addresses that have been</span></span><br><span class="line"><span class="comment">     * allocated to a device and sent in a Provisioning Data PDU until</span></span><br><span class="line"><span class="comment">     * the Provisioner receives an Unprovisioned Device beacon or</span></span><br><span class="line"><span class="comment">     * Service Data for the Mesh Provisioning Service from that same</span></span><br><span class="line"><span class="comment">     * device, identified using the Device UUID of the device.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 检查此设备是否为重新配置的设备 */</span></span><br><span class="line">    node = provisioner_find_node_with_uuid(link.uuid,<span class="literal">NULL</span>);</span><br><span class="line">    <span class="keyword">if</span> (node) &#123;</span><br><span class="line">        <span class="keyword">if</span> (link.element_num &lt;= node-&gt;element_num) &#123;</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * If the device is provisioned before, but the element number of</span></span><br><span class="line"><span class="comment">             * the device is bigger now, then we treat it as a new device.</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            prev_addr = node-&gt;unicast_addr;</span><br><span class="line">        &#125;</span><br><span class="line">        bt_mesh_provisioner_remove_node(link.uuid);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    max_addr = <span class="number">0x7FFF</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (BLE_MESH_ADDR_IS_UNICAST(prev_addr)) &#123;</span><br><span class="line">        sys_put_be16(prev_addr, &amp;pdu[<span class="number">23</span>]);</span><br><span class="line">        link.unicast_addr = prev_addr;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="type">uint16_t</span> alloc_addr = BLE_MESH_ADDR_UNASSIGNED;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (BLE_MESH_ADDR_IS_UNICAST(link.assign_addr)) &#123;</span><br><span class="line">            alloc_addr = link.assign_addr;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">/* If this device to be provisioned is a new device */</span></span><br><span class="line">            <span class="keyword">if</span> (prov_ctx.curr_alloc_addr == BLE_MESH_ADDR_UNASSIGNED) &#123;</span><br><span class="line">                BT_ERR(<span class="string">&quot;Not enough unicast address to be allocated&quot;</span>);</span><br><span class="line">                <span class="keyword">goto</span> fail;</span><br><span class="line">            &#125;</span><br><span class="line">            alloc_addr = prov_ctx.curr_alloc_addr;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (alloc_addr + link.element_num - <span class="number">1</span> &gt; max_addr) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Not enough unicast address for the device&quot;</span>);</span><br><span class="line">            <span class="keyword">goto</span> fail;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 0</span></span><br><span class="line">        <span class="comment">/* Make sure the assigned unicast address is not identical with any unicast</span></span><br><span class="line"><span class="comment">         * address of other nodes. And make sure the address is not identical with</span></span><br><span class="line"><span class="comment">         * any unicast address of Provisioner.</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">if</span> (bt_mesh_provisioner_check_is_addr_dup(alloc_addr, link.element_num, <span class="literal">true</span>)) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Duplicate assigned address 0x%04x&quot;</span>, alloc_addr);</span><br><span class="line">            <span class="keyword">goto</span> fail;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">        sys_put_be16(alloc_addr, &amp;pdu[<span class="number">23</span>]);</span><br><span class="line">        link.unicast_addr = alloc_addr;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    prov_buf_init(buf, PROV_DATA);</span><br><span class="line">	<span class="comment">//配网数据加密</span></span><br><span class="line">    err = bt_mesh_prov_encrypt(session_key, nonce, pdu, net_buf_simple_add(buf, <span class="number">33</span>));</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to encrypt provisioning data&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//发送加密后的配网数据</span></span><br><span class="line">    <span class="keyword">if</span> (prov_send(buf)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to send Provisioning Data&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * We update the next unicast address to be allocated here because if</span></span><br><span class="line"><span class="comment">     * Provisioner is provisioning two devices at the same time, we need</span></span><br><span class="line"><span class="comment">     * to assign the unicast address for them correctly. Hence we should</span></span><br><span class="line"><span class="comment">     * not update the prov_ctx.curr_alloc_addr after the proper provisioning</span></span><br><span class="line"><span class="comment">     * complete pdu is received.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 1</span></span><br><span class="line">    <span class="keyword">if</span> (!BLE_MESH_ADDR_IS_UNICAST(prev_addr)) &#123;</span><br><span class="line">        <span class="keyword">if</span> (BLE_MESH_ADDR_IS_UNICAST(link.assign_addr)) &#123;</span><br><span class="line">            <span class="comment">/* Even if the unicast address of the node is assigned by the</span></span><br><span class="line"><span class="comment">             * application, we will also update the prov_ctx.curr_alloc_addr</span></span><br><span class="line"><span class="comment">             * here, in case Users use the two methods together (i.e. allocate</span></span><br><span class="line"><span class="comment">             * the unicast address for the node internally and assign the</span></span><br><span class="line"><span class="comment">             * unicast address for the node from application).</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            <span class="keyword">if</span> (prov_ctx.curr_alloc_addr &lt; link.assign_addr + link.element_num) &#123;</span><br><span class="line">                prov_ctx.curr_alloc_addr = link.assign_addr + link.element_num;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            prov_ctx.curr_alloc_addr += link.element_num;</span><br><span class="line">            <span class="keyword">if</span> (prov_ctx.curr_alloc_addr &gt; max_addr) &#123;</span><br><span class="line">                <span class="comment">/* No unicast address will be used for further provisioning */</span></span><br><span class="line">                prov_ctx.curr_alloc_addr = <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> 0</span></span><br><span class="line">        <span class="comment">/* Store the available unicast address range to flash */</span></span><br><span class="line">        <span class="keyword">if</span> (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) &#123;</span><br><span class="line">            bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr);</span><br><span class="line">        &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    link.ki_flags = prov_ctx.curr_flags;</span><br><span class="line">    link.iv_index = prov_ctx.curr_iv_index;</span><br><span class="line">    </span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">    link.expect = PROV_COMPLETE;</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">fail:</span><br><span class="line">    close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//接收到对方随机值进入</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_random</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> conf_verify[<span class="number">16</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">    <span class="comment">//双方随机值比较 不一样才对 </span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="built_in">memcmp</span>(data, link.rand, <span class="number">16</span>)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Random value is identical to ours, rejecting.&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//通过未配网设备发来的随机数生成确认值</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_prov_conf(link.conf_key, data, link.auth, conf_verify)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to calculate confirmation verification&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//拿未配网设备发来的确认值和配网器通过随机数生成的确认值是否相等</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">memcmp</span>(conf_verify, link.conf, <span class="number">16</span>)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid confirmation value&quot;</span>);</span><br><span class="line">        BT_INFO(<span class="string">&quot;Received:   %s&quot;</span>, bt_hex(link.conf, <span class="number">16</span>));</span><br><span class="line">        BT_INFO(<span class="string">&quot;Calculated: %s&quot;</span>,  bt_hex(conf_verify, <span class="number">16</span>));</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/** After provisioner receives provisioning random from device,</span></span><br><span class="line"><span class="comment">     *  and successfully check the confirmation, the following</span></span><br><span class="line"><span class="comment">     *  should be done:</span></span><br><span class="line"><span class="comment">     *  1. bt_mesh_calloc memory for prov_salt</span></span><br><span class="line"><span class="comment">     *  2. calculate prov_salt</span></span><br><span class="line"><span class="comment">     *  3. prepare provisioning data and send</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="comment">//生成配网佐料 -&gt; link.prov_salt</span></span><br><span class="line">    <span class="keyword">if</span> (bt_mesh_prov_salt(link.conf_salt, link.rand, data,link.prov_salt)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to generate ProvisioningSalt&quot;</span>);</span><br><span class="line">        <span class="keyword">goto</span> fail;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;ProvisioningSalt: %s&quot;</span>, bt_hex(link.prov_salt, <span class="number">16</span>));</span><br><span class="line">	<span class="comment">//发送配网数据</span></span><br><span class="line">    send_prov_data();</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">fail:</span><br><span class="line">    close_link(PROV_ERR_UNEXP_ERR, CLOSE_REASON_FAILED);</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_data</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;prov_data\r\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//收到配网完成进入</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_complete</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">uint8_t</span> device_key[<span class="number">16</span>] = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">    <span class="type">uint16_t</span> net_idx = <span class="number">0U</span>;</span><br><span class="line">    <span class="comment">//uint16_t index = 0U;</span></span><br><span class="line">    <span class="type">int</span> err = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">//int i;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//设备密钥</span></span><br><span class="line">    err = bt_mesh_dev_key(link.dhkey, link.prov_salt, device_key);</span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to generate device key&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_NONE, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    net_idx = prov_ctx.curr_net_idx;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//保存节点数据</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    err = bt_mesh_provisioner_provision(&amp;link[idx].addr, link[idx].uuid, link[idx].oob_info,</span></span><br><span class="line"><span class="comment">                                        link[idx].unicast_addr, link[idx].element_num, net_idx,</span></span><br><span class="line"><span class="comment">                                        link[idx].ki_flags, link[idx].iv_index, device_key, &amp;index);</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    if (prov-&gt;prov_complete) &#123;</span></span><br><span class="line"><span class="comment">        prov-&gt;prov_complete(index, link[idx].uuid, link[idx].unicast_addr,</span></span><br><span class="line"><span class="comment">                            link[idx].element_num, net_idx);</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    for (i = 0; i &lt; ARRAY_SIZE(unprov_dev); i++) &#123;</span></span><br><span class="line"><span class="comment">        if (!memcmp(unprov_dev[i].uuid, link[idx].uuid, 16) &amp;&amp;</span></span><br><span class="line"><span class="comment">            (unprov_dev[i].flags &amp; RM_AFTER_PROV)) &#123;</span></span><br><span class="line"><span class="comment">            memset(&amp;unprov_dev[i], 0, sizeof(struct unprov_dev_queue));</span></span><br><span class="line"><span class="comment">            break;</span></span><br><span class="line"><span class="comment">        &#125;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    close_link(PROV_ERR_NONE, CLOSE_REASON_SUCCESS);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_failed</span><span class="params">(<span class="type">const</span> <span class="type">u8_t</span> *data)</span></span><br><span class="line">&#123;</span><br><span class="line">    BT_WARN(<span class="string">&quot;Error: 0x%02x&quot;</span>, data[<span class="number">0</span>]);</span><br><span class="line">    close_link(PROV_ERR_NONE, CLOSE_REASON_FAILED);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">const</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">    <span class="type">void</span> (*func)(<span class="type">const</span> <span class="type">u8_t</span> *data);</span><br><span class="line">    <span class="type">u16_t</span> len;</span><br><span class="line">&#125; prov_handlers[] = &#123;</span><br><span class="line">    &#123; prov_invite, <span class="number">1</span> &#125;,</span><br><span class="line">    &#123; prov_capabilities, <span class="number">11</span> &#125;,</span><br><span class="line">    &#123; prov_start, <span class="number">5</span>, &#125;,</span><br><span class="line">    &#123; prov_pub_key, <span class="number">64</span> &#125;,</span><br><span class="line">    &#123; prov_input_complete, <span class="number">0</span> &#125;,</span><br><span class="line">    &#123; prov_confirm, <span class="number">16</span> &#125;,</span><br><span class="line">    &#123; prov_random, <span class="number">16</span> &#125;,</span><br><span class="line">    &#123; prov_data, <span class="number">33</span> &#125;,</span><br><span class="line">    &#123; prov_complete, <span class="number">0</span> &#125;,</span><br><span class="line">    &#123; prov_failed, <span class="number">1</span> &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">close_link</span><span class="params">(<span class="type">u8_t</span> err, <span class="type">u8_t</span> reason)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//PROV_D(&quot;, 10---&gt;0&quot;);</span></span><br><span class="line">    BT_INFO(<span class="string">&quot;close link 10---&gt;0&quot;</span>);</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_GATT)</span></span><br><span class="line">    <span class="keyword">if</span> (link.conn) &#123;</span><br><span class="line">        bt_mesh_pb_gatt_close(link.conn);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line">    <span class="keyword">if</span> (err) &#123;</span><br><span class="line">        prov_send_fail_msg(err);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    link.rx.seg = <span class="number">0</span>;</span><br><span class="line">    bearer_ctl_send(LINK_CLOSE, &amp;reason, <span class="keyword">sizeof</span>(reason));</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    atomic_clear_bit(link.flags, LINK_ACTIVE);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Disable Attention Timer if it was set */</span></span><br><span class="line">    <span class="keyword">if</span> (link.conf_inputs[<span class="number">0</span>]) &#123;</span><br><span class="line">        bt_mesh_attention(<span class="literal">NULL</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_retransmit</span><span class="params">(<span class="keyword">struct</span> k_work *work)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!atomic_test_bit(link.flags, LINK_ACTIVE)) &#123;</span><br><span class="line">        BT_WARN(<span class="string">&quot;Link not active&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (k_uptime_get() - link.tx.start &gt; TRANSACTION_TIMEOUT) &#123;</span><br><span class="line">        BT_WARN(<span class="string">&quot;Giving up transaction&quot;</span>);</span><br><span class="line">        reset_link();</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (link.send_link_close &amp; BIT(<span class="number">0</span>)) &#123;</span><br><span class="line">        <span class="comment">//uint8_t reason = (link.send_link_close &gt;&gt; 1) &amp; BIT_MASK(2);</span></span><br><span class="line">        <span class="type">uint16_t</span> count = (link.send_link_close &gt;&gt; <span class="number">3</span>);</span><br><span class="line">        <span class="keyword">if</span> (count &gt;= <span class="number">2</span>) &#123;</span><br><span class="line">            reset_link();</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        link.send_link_close += BIT(<span class="number">3</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; ARRAY_SIZE(link.tx.buf); i++) &#123;</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">net_buf</span> *<span class="title">buf</span> =</span> link.tx.buf[i];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!buf) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (BT_MESH_ADV(buf)-&gt;busy) &#123;</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%u bytes: %s\r\n&quot;</span>, buf-&gt;len, bt_hex(buf-&gt;data, buf-&gt;len));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (i + <span class="number">1</span> &lt; ARRAY_SIZE(link.tx.buf) &amp;&amp; link.tx.buf[i + <span class="number">1</span>]) &#123;</span><br><span class="line">            bt_mesh_adv_send(buf, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            bt_mesh_adv_send(buf, &amp;buf_sent_cb, <span class="literal">NULL</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">link_ack</span><span class="params">(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (buf-&gt;len) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid Link ACK length %d&quot;</span>, buf-&gt;len);</span><br><span class="line">        close_link(PROV_ERR_NVAL_PDU, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (link.expect == PROV_CAPABILITIES) &#123;</span><br><span class="line">        BT_INFO(<span class="string">&quot;Link ACK is already received&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//发送邀请</span></span><br><span class="line">    send_invite();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">link_close</span><span class="params">(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//printf(&quot;len %u&quot;, buf-&gt;len);</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;关闭连接\r\n&quot;</span>);</span><br><span class="line">    reset_link();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">gen_prov_ctl</span><span class="params">(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    BT_INFO(<span class="string">&quot;配网器接收到: op 0x%02x len %u&quot;</span>, BEARER_CTL(rx-&gt;gpc), buf-&gt;len);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">switch</span> (BEARER_CTL(rx-&gt;gpc)) &#123;</span><br><span class="line">        <span class="keyword">case</span> LINK_OPEN:</span><br><span class="line"></span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> LINK_ACK:</span><br><span class="line">            <span class="keyword">if</span> (!atomic_test_bit(link.flags, LINK_ACTIVE)) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            link_ack(rx, buf);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> LINK_CLOSE:</span><br><span class="line">            <span class="keyword">if</span> (!atomic_test_bit(link.flags, LINK_ACTIVE)) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            link_close(rx, buf);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            BT_ERR(<span class="string">&quot;Unknown bearer opcode: 0x%02x&quot;</span>, BEARER_CTL(rx-&gt;gpc));</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">prov_msg_recv</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">u8_t</span> type = link.rx.buf-&gt;data[<span class="number">0</span>];</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;type 0x%02x len %u&quot;</span>, type, link.rx.buf-&gt;len);</span><br><span class="line">    BT_INFO(<span class="string">&quot;data %s&quot;</span>, bt_hex(link.rx.buf-&gt;data, link.rx.buf-&gt;len));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!bt_mesh_fcs_check(link.rx.buf, link.rx.fcs)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Incorrect FCS&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (type != PROV_FAILED &amp;&amp; type != link.expect) &#123;</span><br><span class="line">        BT_WARN(<span class="string">&quot;Unexpected msg 0x%02x != 0x%02x&quot;</span>, type, link.expect);</span><br><span class="line">        prov_send_fail_msg(PROV_ERR_UNEXP_PDU);</span><br><span class="line">        <span class="comment">/* added for NODE/PROV/BV-10-C in PTS 7.4.1*/</span></span><br><span class="line">        link.expect = PROV_FAILED;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (type &gt;= ARRAY_SIZE(prov_handlers)) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Unknown provisioning PDU type 0x%02x&quot;</span>, type);</span><br><span class="line">        close_link(PROV_ERR_NVAL_PDU, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="number">1</span> + prov_handlers[type].len != link.rx.buf-&gt;len) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid length %u for type 0x%02x&quot;</span>,</span><br><span class="line">               link.rx.buf-&gt;len, type);</span><br><span class="line">        close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    gen_prov_ack_send(link.rx.id);</span><br><span class="line">    link.rx.prev_id = link.rx.id;</span><br><span class="line">    link.rx.id = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    prov_handlers[type].func(&amp;link.rx.buf-&gt;data[<span class="number">1</span>]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">gen_prov_cont</span><span class="params">(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">u8_t</span> seg = CONT_SEG_INDEX(rx-&gt;gpc);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//printf(&quot;分包len %u, seg_index %u\r\n&quot;, buf-&gt;len, seg);</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!link.rx.seg &amp;&amp; link.rx.prev_id == rx-&gt;xact_id) &#123;</span><br><span class="line">        BT_DBG(<span class="string">&quot;Resending ack&quot;</span>);</span><br><span class="line">        gen_prov_ack_send(rx-&gt;xact_id);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (rx-&gt;xact_id != link.rx.id) &#123;</span><br><span class="line">        BT_WARN(<span class="string">&quot;Data for unknown transaction (%u != %u)&quot;</span>,</span><br><span class="line">                rx-&gt;xact_id, link.rx.id);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (seg &gt; link.rx.last_seg) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Invalid segment index %u&quot;</span>, seg);</span><br><span class="line">        close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (seg == link.rx.last_seg) &#123;</span><br><span class="line">        <span class="type">u8_t</span> expect_len;</span><br><span class="line"></span><br><span class="line">        expect_len = (link.rx.buf-&gt;len - <span class="number">20</span> -</span><br><span class="line">                      (<span class="number">23</span> * (link.rx.last_seg - <span class="number">1</span>)));</span><br><span class="line">        <span class="keyword">if</span> (expect_len != buf-&gt;len) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Incorrect last seg len: %u != %u&quot;</span>,</span><br><span class="line">                   expect_len, buf-&gt;len);</span><br><span class="line">            close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!(link.rx.seg &amp; MESH_BIT(seg))) &#123;</span><br><span class="line">        BT_DBG(<span class="string">&quot;Ignoring already received segment&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">memcpy</span>(XACT_SEG_DATA(seg), buf-&gt;data, buf-&gt;len);</span><br><span class="line">    XACT_SEG_RECV(seg);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!link.rx.seg) &#123;</span><br><span class="line">        prov_msg_recv();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">gen_prov_ack</span><span class="params">(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//printf(&quot;应答包\r\n&quot;);</span></span><br><span class="line">    <span class="type">uint8_t</span> ack_type = <span class="number">0U</span>, pub_key_oob = <span class="number">0U</span>;</span><br><span class="line"></span><br><span class="line">    BT_DBG(<span class="string">&quot;len %u&quot;</span>, buf-&gt;len);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!link.tx.buf[<span class="number">0</span>]) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!link.tx.id) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (rx-&gt;xact_id == (link.tx.id - <span class="number">1</span>)) &#123;</span><br><span class="line">        prov_clear_tx();</span><br><span class="line"></span><br><span class="line">        ack_type = link.expect_ack_for;</span><br><span class="line">        <span class="keyword">switch</span> (ack_type) &#123;</span><br><span class="line">        <span class="keyword">case</span> PROV_START:</span><br><span class="line">            pub_key_oob = link.conf_inputs[<span class="number">13</span>];</span><br><span class="line">            send_pub_key(pub_key_oob);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> PROV_PUB_KEY:</span><br><span class="line">            prov_gen_dh_key();</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        link.expect_ack_for = <span class="number">0x00</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">gen_prov_start</span><span class="params">(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (link.rx.seg) &#123;</span><br><span class="line">        BT_DBG(<span class="string">&quot;Got Start while there are unreceived segments&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (link.rx.prev_id == rx-&gt;xact_id) &#123;</span><br><span class="line">        BT_DBG(<span class="string">&quot;Resending ack&quot;</span>);</span><br><span class="line">        gen_prov_ack_send(rx-&gt;xact_id);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    link.rx.buf-&gt;len = net_buf_simple_pull_be16(buf);</span><br><span class="line">    link.rx.id  = rx-&gt;xact_id;</span><br><span class="line">    link.rx.fcs = net_buf_simple_pull_u8(buf);</span><br><span class="line"></span><br><span class="line">    BT_INFO(<span class="string">&quot;len %u last_seg %u total_len %u fcs 0x%02x&quot;</span>, buf-&gt;len,</span><br><span class="line">           START_LAST_SEG(rx-&gt;gpc), link.rx.buf-&gt;len, link.rx.fcs);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (link.rx.buf-&gt;len &lt; <span class="number">1</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Ignoring zero-length provisioning PDU&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (link.rx.buf-&gt;len &gt; link.rx.buf-&gt;size) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Too large provisioning PDU (%u bytes)(%u size)&quot;</span>,</span><br><span class="line">               link.rx.buf-&gt;len,link.rx.buf-&gt;size);</span><br><span class="line">        close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (START_LAST_SEG(rx-&gt;gpc) &gt; <span class="number">0</span> &amp;&amp; link.rx.buf-&gt;len &lt;= <span class="number">20</span>) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Too small total length for multi-segment PDU&quot;</span>);</span><br><span class="line">        close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    link.rx.seg = (<span class="number">1</span> &lt;&lt; (START_LAST_SEG(rx-&gt;gpc) + <span class="number">1</span>)) - <span class="number">1</span>;</span><br><span class="line">    link.rx.last_seg = START_LAST_SEG(rx-&gt;gpc);</span><br><span class="line">    <span class="built_in">memcpy</span>(link.rx.buf-&gt;data, buf-&gt;data, buf-&gt;len);</span><br><span class="line">    XACT_SEG_RECV(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!link.rx.seg) &#123;</span><br><span class="line">        prov_msg_recv();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">const</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">    <span class="type">void</span> (*<span class="type">const</span> func)(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf);</span><br><span class="line">    <span class="type">const</span> <span class="type">u8_t</span> require_link;</span><br><span class="line">    <span class="type">const</span> <span class="type">u8_t</span> min_len;</span><br><span class="line">&#125; gen_prov[] = &#123;</span><br><span class="line">    &#123; gen_prov_start, <span class="literal">true</span>, <span class="number">3</span> &#125;,</span><br><span class="line">    &#123; gen_prov_ack, <span class="literal">true</span>, <span class="number">0</span> &#125;,</span><br><span class="line">    &#123; gen_prov_cont, <span class="literal">true</span>, <span class="number">0</span> &#125;,</span><br><span class="line">    &#123; gen_prov_ctl, <span class="literal">true</span>, <span class="number">0</span> &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">gen_prov_recv</span><span class="params">(<span class="keyword">struct</span> prov_rx *rx, <span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//printf(&quot;GPCF(rx-&gt;gpc) = %d\r\n&quot;,GPCF(rx-&gt;gpc));</span></span><br><span class="line">    <span class="keyword">if</span> (buf-&gt;len &lt; gen_prov[GPCF(rx-&gt;gpc)].min_len) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Too short GPC message type %u&quot;</span>, GPCF(rx-&gt;gpc));</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!atomic_test_bit(link.flags, LINK_ACTIVE) &amp;&amp;</span><br><span class="line">        gen_prov[GPCF(rx-&gt;gpc)].require_link) &#123;</span><br><span class="line">        <span class="comment">//BT_INFO(&quot;Ignoring message that requires active link&quot;);</span></span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    gen_prov[GPCF(rx-&gt;gpc)].func(rx, buf);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">bt_mesh_prov_adv_recv</span><span class="params">(<span class="keyword">struct</span> net_buf_simple *buf)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">prov_rx</span> <span class="title">rx</span>;</span></span><br><span class="line"></span><br><span class="line">    rx.link_id = net_buf_simple_pull_be32(buf);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (buf-&gt;len &lt; <span class="number">2</span>) &#123;</span><br><span class="line">        BT_WARN(<span class="string">&quot;Too short provisioning packet (len %u)&quot;</span>, buf-&gt;len);</span><br><span class="line">        close_link(PROV_ERR_NVAL_PDU, CLOSE_REASON_FAILED);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    rx.xact_id = net_buf_simple_pull_u8(buf);</span><br><span class="line">    rx.gpc = net_buf_simple_pull_u8(buf);</span><br><span class="line"></span><br><span class="line">    gen_prov_recv(&amp;rx, buf);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span> <span class="comment">/* CONFIG_BT_MESH_PB_ADV */</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_prov_active</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> atomic_test_bit(link.flags, LINK_ACTIVE);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">bt_mesh_prov_reset</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;reset) &#123;</span><br><span class="line">        prov-&gt;reset();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">net_key_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//初始化设备密钥，这里用三元组的 secret 作为配网器设备密钥</span></span><br><span class="line">    stringtohex(DEFAULT_SECRET, provisoner_devkey, <span class="number">16</span>);</span><br><span class="line">    bt_mesh_rand(&amp;g_netkey.key[<span class="number">0</span>],<span class="number">16</span>);</span><br><span class="line">    seq = <span class="number">0</span>;</span><br><span class="line">    addr = <span class="number">0</span>;</span><br><span class="line">    g_netkey.net_index = <span class="number">0</span>;</span><br><span class="line">    g_netkey.flag = <span class="number">0</span>;</span><br><span class="line">    g_netkey.ivi = <span class="number">0</span>;</span><br><span class="line">    BT_INFO(<span class="string">&quot;g_netkey:%s&quot;</span>, bt_hex(g_netkey.key,<span class="number">16</span>));</span><br><span class="line">    bt_mesh_provision(g_netkey.key, g_netkey.net_index, g_netkey.flag, g_netkey.ivi, seq, addr, provisoner_devkey);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Dynamically added appkey &amp; netkey will use these key_idx */</span></span><br><span class="line">    <span class="comment">//下一个APP索引和NET索引</span></span><br><span class="line">    bt_mesh.p_app_idx_next = <span class="number">0x0000</span>;</span><br><span class="line">    bt_mesh.p_net_idx_next = <span class="number">0x0001</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//BT_INFO(&quot;netkey:%s&quot;, bt_hex(netkey.key,16));</span></span><br><span class="line">    <span class="comment">//初始化应用密钥</span></span><br><span class="line">    bt_mesh_rand(&amp;appkey.key[<span class="number">0</span>],<span class="number">16</span>);</span><br><span class="line">    appkey.net_index = <span class="number">0</span>;</span><br><span class="line">    appkey.key_index = <span class="number">0</span>;</span><br><span class="line">    appkey.flag = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">extern</span> <span class="type">void</span> <span class="title function_">genie_appkey_register</span><span class="params">(<span class="type">u16_t</span> net_idx, <span class="type">u16_t</span> app_idx, <span class="type">const</span> <span class="type">u8_t</span> val[<span class="number">16</span>], <span class="type">bool</span> update)</span>;</span><br><span class="line">    genie_appkey_register(appkey.net_index, appkey.key_index, appkey.key, appkey.flag);</span><br><span class="line">    BT_INFO(<span class="string">&quot;appkey:%s&quot;</span>, bt_hex(appkey.key,<span class="number">16</span>));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">is_provisioning</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> atomic_test_bit(link.flags, LINK_ACTIVE);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//通过网络索引获取网络密钥</span></span><br><span class="line"><span class="keyword">struct</span> bt_mesh_subnet *<span class="title function_">bt_mesh_provisioner_subnet_get</span><span class="params">(<span class="type">uint16_t</span> net_idx)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_subnet</span> *<span class="title">sub</span> =</span> <span class="literal">NULL</span>;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">    BT_DBG(<span class="string">&quot;%s&quot;</span>, __func__);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (net_idx == BLE_MESH_KEY_ANY) &#123;</span><br><span class="line">        <span class="keyword">return</span> bt_mesh.p_sub[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; ARRAY_SIZE(bt_mesh.p_sub); i++) &#123;</span><br><span class="line">        sub = bt_mesh.p_sub[i];</span><br><span class="line">        <span class="keyword">if</span> (sub &amp;&amp; sub-&gt;net_idx == net_idx) &#123;</span><br><span class="line">            <span class="keyword">return</span> sub;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//初始化配网器配网信息</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_provisioner_init_prov_info</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (prov_ctx.primary_addr == BLE_MESH_ADDR_UNASSIGNED) &#123;</span><br><span class="line">        <span class="comment">/* If unicast address of primary element of Provisioner has not been set</span></span><br><span class="line"><span class="comment">         * before, then the following initialization procedure will be used.</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">if</span> (prov == <span class="literal">NULL</span>) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;No provisioning context provided&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> -EINVAL;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (!BLE_MESH_ADDR_IS_UNICAST(prov-&gt;prov_unicast_addr) ||</span><br><span class="line">            !BLE_MESH_ADDR_IS_UNICAST(prov-&gt;prov_start_address)) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Invalid address, own 0x%04x, start 0x%04x&quot;</span>,</span><br><span class="line">                    prov-&gt;prov_unicast_addr, prov-&gt;prov_start_address);</span><br><span class="line">            <span class="keyword">return</span> -EINVAL;</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="comment">//获取成分数据</span></span><br><span class="line">        <span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_comp</span> *<span class="title">comp</span> =</span> bt_mesh_comp_get();</span><br><span class="line">        <span class="keyword">if</span> (!comp) &#123;</span><br><span class="line">            BT_ERR(<span class="string">&quot;Invalid composition data&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> -EINVAL;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (prov-&gt;prov_unicast_addr + comp-&gt;elem_count &gt; prov-&gt;prov_start_address) &#123;</span><br><span class="line">            BT_WARN(<span class="string">&quot;Too small start address 0x%04x, update to 0x%04x&quot;</span>,</span><br><span class="line">                prov-&gt;prov_start_address, prov-&gt;prov_unicast_addr + comp-&gt;elem_count);</span><br><span class="line">            prov_ctx.curr_alloc_addr = prov-&gt;prov_unicast_addr + comp-&gt;elem_count;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            prov_ctx.curr_alloc_addr = prov-&gt;prov_start_address;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* Update primary element address with the initialized value here. */</span></span><br><span class="line">        prov_ctx.primary_addr = prov-&gt;prov_unicast_addr;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) &#123;</span></span><br><span class="line">        <span class="comment">//    bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.curr_alloc_addr);</span></span><br><span class="line">        <span class="comment">//&#125;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    prov_ctx.curr_net_idx = BLE_MESH_KEY_PRIMARY;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bt_mesh_subnet</span> *<span class="title">sub</span> =</span> bt_mesh_provisioner_subnet_get(BLE_MESH_KEY_PRIMARY);</span><br><span class="line">    prov_ctx.curr_flags = bt_mesh_net_flags(sub);</span><br><span class="line">    prov_ctx.curr_iv_index = bt_mesh.iv_index;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//配网器初始化</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">bt_mesh_provisioner_prov_init</span><span class="params">(<span class="type">const</span> <span class="keyword">struct</span> bt_mesh_prov *prov_info)</span></span><br><span class="line">&#123;</span><br><span class="line">    BT_INFO(<span class="string">&quot;配网器初始化&quot;</span>);</span><br><span class="line">    <span class="type">const</span> <span class="type">uint8_t</span> *key = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!prov_info) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;No provisioning context provided&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> -EINVAL;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//获得公钥</span></span><br><span class="line">    key = bt_mesh_pub_key_get();</span><br><span class="line">    <span class="keyword">if</span> (!key) &#123;</span><br><span class="line">        BT_ERR(<span class="string">&quot;Failed to generate Public Key&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> -EIO;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    prov = prov_info;</span><br><span class="line"></span><br><span class="line">    prov_ctx.primary_addr = BLE_MESH_ADDR_UNASSIGNED;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (prov-&gt;static_val &amp;&amp; prov-&gt;static_val_len) &#123;</span><br><span class="line">        prov_ctx.static_oob_len = prov-&gt;static_val_len &gt; <span class="number">16</span> ? <span class="number">16</span> : prov-&gt;static_val_len;</span><br><span class="line">        <span class="built_in">memcpy</span>(prov_ctx.static_oob_val, prov-&gt;static_val, prov_ctx.static_oob_len);</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//网络密钥初始化</span></span><br><span class="line">    net_key_init();</span><br><span class="line">    <span class="comment">//配网器配网信息初始化</span></span><br><span class="line">    bt_mesh_provisioner_init_prov_info();</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> defined(CONFIG_BT_MESH_PB_ADV)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//struct prov_adv_buf *adv = &amp;adv_buf;</span></span><br><span class="line">    <span class="comment">//adv-&gt;buf.size = ADV_BUF_SIZE;</span></span><br><span class="line">    <span class="comment">//adv-&gt;buf.__buf = adv_buf_data + ADV_BUF_SIZE;</span></span><br><span class="line">	<span class="comment">//初始化重传计数器</span></span><br><span class="line">    k_delayed_work_init(&amp;link.tx.retransmit, prov_retransmit);</span><br><span class="line">	<span class="comment">//初始化上次接收ID和接收缓存</span></span><br><span class="line">    link.rx.prev_id = XACT_NVAL;</span><br><span class="line">    link.rx.buf = bt_mesh_pba_get_buf();</span><br><span class="line">    </span><br><span class="line"><span class="meta">#<span class="keyword">endif</span><span class="comment">/*CONFIG_BT_MESH_PB_ADV*/</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="四、测试现象"><a href="#四、测试现象" class="headerlink" title="四、测试现象"></a><font size=3>四、测试现象</font></h1><p>上面的代码可能还是有bug，而且仅仅是实现了配网器功能，没有自组网，没有任何控制功能：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/LV04-%E5%A4%A9%E7%8C%AB%E8%93%9D%E7%89%99Mesh%E9%A1%B9%E7%9B%AE-02-%E8%BF%9B%E9%98%B6%E5%BC%80%E5%8F%91-09-%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%91%E5%99%A8%E4%BB%A3%E7%A0%81/img/image-20240107111752251.png" alt="image-20240107111752251" />

<p>反正看打印信息，是存在配网器对未配网设备进行配网的过程的，但是最后好像是没有配成功，因为我看到右侧的配网器一直能发现未配网设备，左侧的未配网设备也是一直和配网器配网，最终出现内存不足的情况，具体的bug没有去查，因为毕竟目前还是在做嵌入式开发，蓝牙这块暂时没有相关的需求，这几篇笔记也仅仅是想简单了解一下蓝牙，后续有需求再深入研究吧。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/a6277951.html">LV04-天猫蓝牙Mesh项目-02-进阶开发-09-实现配网器代码</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年01月07日 - 11:46</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/a6277951.html" title="LV04-天猫蓝牙Mesh项目-02-进阶开发-09-实现配网器代码">https://sumumm.github.io/post/a6277951.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV21-%E5%A4%A9%E7%8C%AB%E7%B2%BE%E7%81%B5IOT/" rel="tag"><i class="fa fa-tag"></i> LV21-天猫精灵IOT</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/9f66e555.html" rel="prev" title="LV04-天猫蓝牙Mesh项目-03-归纳总结">
                  <i class="fa fa-angle-left"></i> LV04-天猫蓝牙Mesh项目-03-归纳总结
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/338b2ddc.html" rel="next" title="LV04-天猫蓝牙Mesh项目-02-进阶开发-08-手机连接Mesh节点控制">
                  LV04-天猫蓝牙Mesh项目-02-进阶开发-08-手机连接Mesh节点控制 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
